【问题标题】:creating int[] array (with positive integers only) from String从 String 创建 int[] 数组(仅限正整数)
【发布时间】:2013-12-23 16:15:33
【问题描述】:

嘿,我在从字符串创建 int[] 数组时遇到问题,它应该只包含来自字符串的正整数。

它应该做这样的事情:

INPUT: ("-5, 20,  abc, -20, defg, 45ab67, 23")
OUTPUT: [20,23]

我不知道如何删除这个子字符串“45ab67”。我尝试使用

Character.isDigit(c);
and
myString.replaceAll("[^\\d,-]", "");

效果不太好……

提前谢谢

【问题讨论】:

    标签: java arrays string substring replaceall


    【解决方案1】:

    试试,

     String input = "-5, 20, abc, -20, defg, 45ab67, 23";
     String[] arr = input.split(",");
     for (String num : arr) {
          if (num.trim().matches("\\d+")) {
            System.out.println(num);
          }
     }
    

    【讨论】:

    • 难道你不需要 ^ 和 $ 来防止包含数字的字符串匹配吗?
    • @TimB, String.matches 不需要边界符号 ^ 和 $。
    • 我愿意相信你,但在 Javadoc 中没有提到这一点 :) docs.oracle.com/javase/6/docs/api/java/lang/…
    • @TimB,是的,文档没有提到它。如果您使用Pattern.compile(),则需要使用边界符号。
    【解决方案2】:

    1) 将字符串拆分为 , 2)解析try-catch中的部分(Integer.parseInt(来自每个部分);) 3)如果没有 NumberFormatException 则检查结果是否> 0,如果是,则将其添加到最终列表中

    【讨论】:

      【解决方案3】:

      这应该可以完成工作:

      String input = "-5, 20,  abc, -20, defg, 45ab67, 23"
      String[] inputs = input.split(",");
      
      int[] outputArray;
      List<Integer> outputList = new ArrayList<>();
      
      for (String in : inputs) {
          if (in.trim().matches("\\d+")) {
              outputList.add(Integer.parseInt(in.trim()));
          }
      }
      
      outputArray = outputList.toArray(new Integer[outputList.size()]);
      
      System.out.println(outputArray);
      

      【讨论】:

      • 您需要在解析之前对字符串进行修剪()。并且 outputArray 必须是 Integer[]
      • 自动装箱对outputArray 不起作用吗?感谢.trim() 提醒。
      【解决方案4】:
      List<Integer> list = new ArrayList<Integer>();
      for (String s: myString.split(" *, *")) {
          if (s.matches("^[0-9]+$")) {
              list.add(Integer.parseInt(s));
          }
      }
      int[] result = new int[list.size()];
      int i = 0;
      for (Integer v: list) {
          result[i++] = v;
      }
      

      【讨论】:

        【解决方案5】:

        试试这个

            Matcher m = Pattern.compile("(^| )(\\d+)(,|$)").matcher(s);
            while(m.find()) {
                System.out.println(m.group(2));
            }
        

        【讨论】:

          【解决方案6】:
          int[] getArray(String s) {
              String[] parts = s.split(", ");
              ArrayList<Integer> resultArray = new ArrayList<>();
              for (String part : parts) {
                  if (part.matches("\\d+")) {
                      resultArray.add(Integer.parseInt(part));
                  }
              }
              int[] result = new int[resultArray.size()];
              for (int i = 0; i < resultArray.size(); i++) {
                  result[i] = resultArray.get(i);
              }
              return result;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-06-01
            • 1970-01-01
            • 2022-01-02
            • 1970-01-01
            • 1970-01-01
            • 2016-05-02
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多