我有文本数据,我想拆分String
和int
数组Java
String text = "one1two20three300four4000";
期望的输出:
String str[] = "one","two","three","four"; int num[] = 1,20,300,4000;
TheLostMind.. 5
这个适用于Java:
public static void main(String[] args) { String text = "one1two20three300four4000"; String arr1[] = text.replaceFirst("^\\d+", "").split("\\d+"); String arr2[] = text.replaceFirst("^\\D+", "").split("\\D+"); System.out.println(Arrays.toString(arr1)); System.out.println(Arrays.toString(arr2)); // parse each value as an in using Streams (preferably) or a loop. }
O/P:
[
1,2,3,4 ] [1,20,300,4000]
PS:在java 8中int[] arr2Int = Arrays.stream(arr2).mapToInt(Integer::parseInt).toArray();
用于将String数组转换为int数组.
这个适用于Java:
public static void main(String[] args) { String text = "one1two20three300four4000"; String arr1[] = text.replaceFirst("^\\d+", "").split("\\d+"); String arr2[] = text.replaceFirst("^\\D+", "").split("\\D+"); System.out.println(Arrays.toString(arr1)); System.out.println(Arrays.toString(arr2)); // parse each value as an in using Streams (preferably) or a loop. }
O/P:
[
1,2,3,4 ] [1,20,300,4000]
PS:在java 8中int[] arr2Int = Arrays.stream(arr2).mapToInt(Integer::parseInt).toArray();
用于将String数组转换为int数组.