【问题标题】:Using indexOf() twice fails to detect second character occurence使用 indexOf() 两次未能检测到第二个字符的出现
【发布时间】:2013-07-21 00:24:15
【问题描述】:

我有一个字符串,例如 9.555.555,00,我想删除所有符号并只保留数字,这是字符串格式。

我使用 indexof 来查找特殊字符,然后使用循环来在循环到达特定字符时跳过特定字符,这样它就不会将该字符附加到最终字符串中。

但是,在执行此操作时,代码似乎选择了第一次出现的句点符号,但 indexOf() 第二次返回 -1,即使还有另一个 .在字符串中。

int dotIndex, commaIndex;

        dotIndex = tempBalance.indexOf('.');
        commaIndex = tempBalance.indexOf(',');

        for(int j = 0; j < tempBalance.length(); ++j){

            //System.out.println("Iteration: " + j + " ~ i @ : " + i);

            if(j == dotIndex){
                System.out.println("Current dot Index: " + dotIndex + " J: " + j + " : " + tempBalance);
                dotIndex = tempBalance.indexOf(j+1, '.');
                System.out.println("New dotIndex: " + dotIndex);
                continue;
            } else if(j == commaIndex){
                break;
            } else {

                tempString.append(tempBalance.charAt(j));
                //System.out.print("Found normal Number: " + tempBalance.substring(j, (j+1)));
            }

system.out.println 的输出:

当前点索​​引:1 J:1:9.955.458,23
新点索引:-1

【问题讨论】:

  • @radimpe 看来replace 应该是replaceAll
  • @AndrewThompson。不,replace() 可以正常工作。它还替换了所有出现的事件。 replaceAll() 相反用于替换正则表达式模式。
  • @RohitJain 感谢您的更正。看来我错了。 :P
  • indexOf() 返回第一次出现的索引
  • tempBalance.replace(".","").replace(",","") 可以。但是 replaceAll(例如tempBalance.replaceAll(".","").replaceAll(",",""))不起作用并返回空白响应。当char 可以这样做时,我可能会因为将它们设置为String 而感到“错误”。

标签: java string loops


【解决方案1】:
dotIndex = tempBalance.indexOf(j+1, '.');

应该是

dotIndex = tempBalance.indexOf('.', j+1);

但这不是唯一的问题。一旦你修复了'.'。您仍然需要整理解析所有','。简单地修复上述问题仍然只会返回 9955458 减去 23

【讨论】:

  • 非常正确,但是我真的不需要最后一个值,因为它们是美分,如果我将它们附加到整数,它们的值就会改变。
【解决方案2】:

你应该在indexOf中切换参数位置:

dotIndex = tempBalance.indexOf('.', j + 1);

第一个参数是要搜索的字符,第二个是要开始的索引。

【讨论】:

    【解决方案3】:

    tempBalance.indexOf(j+1, '.') 应该是 tempBalance.indexOf('.', j+1)。 见Javadocs for indexOf(int, int)。

    【讨论】:

      【解决方案4】:

      要删除非数字字符,您可以这样做-

      public static String removeNonDigits(String input) {
          StringBuilder result = new StringBuilder();
          for (int i = 0; i < input.length(); i++) {
              char c = input.charAt(i);
              if (Character.isDigit(c)) {
                  result.append(c);
              }
          }
          return result.toString();
      }
      

      此外,对于数字格式,您可以使用 NumberFormat 和区域设置。

      【讨论】:

        【解决方案5】:
        want to remove all the symbols(.,) and just keep the numbers
        

        使用正则表达式:

        String exprStr="9.555.555,00";
        exprStr=exprStr.replaceAll("[.,]","");
        System.out.println(exprStr);
        

        其中 [.,] 是标识 . 或 ,

        的正则表达式

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-09-26
          • 2018-02-05
          • 1970-01-01
          • 2022-10-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-30
          相关资源
          最近更新 更多