【问题标题】:Extracting integers from a String into an Array从字符串中提取整数到数组中
【发布时间】:2019-03-09 18:40:30
【问题描述】:

我需要从 String 中提取 整数 到一个数组中。

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

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

【问题讨论】:

标签: java arrays regex string extract


【解决方案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]));
}

【讨论】:

    【解决方案2】:
    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);
    
            }
    
    }
    

    【讨论】:

      【解决方案3】:

      不要用空字符串替换字符,而是用空格替换。然后分开。

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

      【讨论】:

        【解决方案4】:

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

        输出相同。

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

            【解决方案6】:

            工作代码:

            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
            }
            

            【讨论】:

              猜你喜欢
              • 2020-07-20
              • 2011-01-22
              • 2019-09-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多