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

问题描述 投票:0回答:6

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

我已经有了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;
}

PS:这是最明显的答案。感谢Pavan Andhukuri

String numbersLine = line.replaceAll("[^0-9]+", " ");
java arrays regex string extract
6个回答
1
投票

而不是用空字符串代替字符,而用空格代替。然后将其拆分。

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;
    }
}

2
投票

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

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,但这也是您的要求。


1
投票

鉴于您拥有数字,例如“ 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]));
}

0
投票
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);

        }

}

0
投票

您可以尝试使用流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();

输出相同的地方。


0
投票

工作代码:

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
}
© www.soinside.com 2019 - 2024. All rights reserved.