将字符串中的整数提取到数组中

Extracting integers from a String into an Array

我需要从 String 中提取 integers 到数组中。

我已经得到 整数,但我无法将它们放入数组中。

public static void main(String[] args) {
    String line = "First number 10, Second number 25, Third number 123";
    String numbersLine = line.replaceAll("[^0-9]+", "");
    int result = Integer.parseInt(numbersLine);

    // What I want to get:
    // array[0] = 10;
    // array[1] = 25;
    // array[2] = 123;
}

给定数字字符串,例如“10, 20, 30”,您可以使用以下内容:

String numbers = "10, 20, 30";

String[] numArray = nums.split(", ");

ArrayList<Integer> integerList = new ArrayList<>();

for (int i = 0; i < x.length; i++) {
    integerList.add(Integer.parseInt(numArray[i]));
}
public static void main(String args[]) {

        String line = "First number 10, Second number 25, Third number 123 ";

        String[] aa=line.split(",");
        for(String a:aa)
        {
            String numbersLine = a.replaceAll("[^0-9]+", "");
            int result = Integer.parseInt(numbersLine);
            System.out.println(result);

        }

}

不是用空字符串替换字符,而是用 space 替换。然后分裂它。

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String line = "First number 10, Second number 25, Third number 123 ";
        String numbersLine = line.replaceAll("[^0-9]+", " ");

        String[] strArray = numbersLine.split(" ");

        List<Integer> intArrayList = new ArrayList<>();

        for (String string : strArray) {
            if (!string.equals("")) {
                System.out.println(string);
                intArrayList.add(Integer.parseInt(string));
            }
        }

        // what I want to get:
        // int[0] array = 10;
        // int[1] array = 25;
        // int[2] array = 123;
    }
}

您可以尝试使用流 api:

String input = "First number 10, Second number 25, Third number 123";

int[] anArray = Arrays.stream(input.split(",? "))
    .map(s -> {
        try {
            return Integer.valueOf(s);
        } catch (NumberFormatException ignored) {
            return null;
        }
    })
    .filter(Objects::nonNull)
    .mapToInt(x -> x)
    .toArray();


System.out.println(Arrays.toString(anArray));

输出为:

[10, 25, 123]

regex.replaceAll 版本将是:

int[] a = Arrays.stream(input.replaceAll("[^0-9]+", " ").split(" "))
    .filter(x -> !x.equals(""))
    .map(Integer::valueOf)
    .mapToInt(x -> x)
    .toArray();

输出相同。

您可以使用正则表达式来提取数字:

String s = "First number 10, Second number 25, Third number 123 ";
Matcher matcher = Pattern.compile("\d+").matcher(s);

List<Integer> numbers = new ArrayList<>();
while (matcher.find()) {
    numbers.add(Integer.valueOf(matcher.group()));
}

\d+代表任何数字重复一次或多次。

如果循环输出,您将得到:

numbers.forEach(System.out::println);

// 10
// 25
// 123

注意:此解决方案仅适用于 Integer,但这也是您的要求。

工作代码:

public static void main(String[] args) 
{
    String line = "First number 10, Second number 25, Third number 123 ";
    String[] strArray= line.split(",");
    int[] integerArray =new int[strArray.length];

    for(int i=0;i<strArray.length;i++)
        integerArray[i]=Integer.parseInt(strArray[i].replaceAll("[^0-9]", ""));
    for(int i=0;i<integerArray.length;i++)
        System.out.println(integerArray[i]);
        //10
        //15 
        //123
}