【问题标题】:Get second number from this line of text too也从这行文本中获取第二个数字
【发布时间】:2017-04-23 21:50:52
【问题描述】:

目前我只能返回 40。 我也想要第二个数字(在本例中为 1,600,077.17) 但之后没有任何东西,例如“1,000.00”或“23”或“70”。
请记住,字符串“str”只是一个示例值,有时会发生变化。

public static String extractNumber1(final String line) { String str = "cows 40 1,600,077.17 1,000.00 23 70"; if(str == null || str.isEmpty()) return ""; StringBuilder sb = new StringBuilder(); boolean found = false; for(char c : str.toCharArray()){ if(Character.isDigit(c)){ sb.append(c); found = true; } else if(found){ // If we already found a digit before and this char is not a digit, stop looping break; } } return sb.toString(); }

【问题讨论】:

  • @Ryan 谢谢。
  • 添加一个regex 标签会吸引正则表达式大师在这里..

标签: java file text jtextarea


【解决方案1】:

使用

string[] strAr = str.Split(' ');

将您的字符串转换为字符串数组。 strAr[2] 将是 1,600,077.17

【讨论】:

    【解决方案2】:

    我会在使用空格之前拆分字符串。例如)String[] strings = str.split(" ")。然后只需检查数组中的每个值的数字。然后,您可以轻松选择要保留的值。

    【讨论】:

    • 这是效果最好的一个,但我发现有时空间的数量也会发生变化。有时它仍然会在第二个之后获得其他数字。有时它会获取第二个数字并返回两次。
    【解决方案3】:

    你可以使用int found = 0;

    每找到一个数字,增加找到found++

    将else if改为if(found==2),去掉“else”

    完整代码:

    public static String extractNumber1(final String line) {                
    String str = "cows 40 1,600,077.17 1,000.00 23 70"; 
    
        if(str == null || str.isEmpty()) return "";
    
        StringBuilder sb = new StringBuilder();
        int found = 0;
        for(char c : str.toCharArray()){
            if(Character.isDigit(c)){
                sb.append(c);
                found++;
            } if(found==2){
                // If we already found a digit before and this char is not a digit, stop looping
                break;                
            }
        }
    
        return sb.toString();
        }
    

    编辑:删除 else

    【讨论】:

      【解决方案4】:

      regular expression解析行:

      String str = "cows 40 1,600,077.17 1,000.00 23 70";
      
      Pattern p = Pattern.compile("(\\w+) (\\d+) ([0-9,.]+) ([0-9,.]+) (\\d+) (\\d+)");
      Matcher m = p.matcher(str);
      if (m.matches()) {
          for (int i = 1; i <= m.groupCount(); i++)
              System.out.println(i + ": " + m.group(i));
      }
      

      输出

      1: cows
      2: 40
      3: 1,600,077.17
      4: 1,000.00
      5: 23
      6: 70
      

      例如,如果您想获得前两个数字,就像问题所说的那样,并且您希望它们是实际数字,而不是字符串,请在 if 语句中执行此操作:

      NumberFormat fmt = NumberFormat.getInstance();
      int    firstNumber  = Integer.parseInt(m.group(2));
      double secondNumber = fmt.parse(m.group(3)).doubleValue();
      System.out.printf("1st = %d  2nd = %.2f%n", firstNumber, secondNumber);
      

      输出

      1st = 40  2nd = 1600077.17
      

      【讨论】:

        【解决方案5】:

        你可以把found变成int,当你找到一个数字时增加它,然后在found = 2上打断。

        不过我会使用不同的方法,我会拆分字符串并检查部分是否是尝试转换为双精度的数字

        public static String extractNumber1(final String line) {                
        
            String str = "cows 40 1,600,077.17 1,000.00 23 70"; 
        
            if(str == null || str.isEmpty()) return "";
        
            String[] parts = str.split(" ");
        
            StringBuilder sb = new StringBuilder();
            int found = 0;
            int i = 0;
            while(i < parts.length && found < 2){
                try {
                    long x = Long.parseLong(parts[i]);
                    found++;
                    sb.append(parts[i]);
                } catch(Exception e) {
                } finally {
                    i++; // continue with the loop without breaking
                }
            }  
            return sb.toString();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-22
          • 1970-01-01
          • 2018-07-13
          • 2018-06-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多