我想分成`String和`int`数组Java

时间:2015-12-29 06:16:00

标签: java arrays string int

我有文本数据,我想分成Stringint数组Java

String text = "one1two20three300four4000";

期望的输出:

String str[] = "one","two","three","four";

int num[] = 1,20,300,4000;

1 个答案:

答案 0 :(得分: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数组。