string translate to array int use java tool, such as StringUtils or Arrary
时间: 2024-03-24 14:37:18 浏览: 12
You can use the `StringUtils` class from the Apache Commons Lang library to convert a String to an array of integers in Java.
Here's an example code snippet that uses `StringUtils` to convert a String to an array of integers:
```
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String str = "12345";
int[] arr = StringUtils.toCodePoints(str).toArray();
System.out.println(Arrays.toString(arr)); // Output: [49, 50, 51, 52, 53]
}
}
```
In this code, we first import the `StringUtils` class from the Apache Commons Lang library and the `Arrays` class from the Java standard library.
We then create a String `str` with the value we want to convert to an array of integers. We use the `StringUtils.toCodePoints()` method to convert the String to a stream of Unicode code points, and then use the `toArray()` method to convert the stream to an integer array.
Finally, we use the `Arrays.toString()` method to print the integer array to the console.
Note that the `StringUtils.toCodePoints()` method returns a `IntStream`, so we need to use the `toArray()` method to convert it to an integer array.