【问题标题】:How do I read and remove a number from a string?如何从字符串中读取和删除数字?
【发布时间】:2015-12-10 18:50:50
【问题描述】:

例如,我有这个字符串:

0no1no2yes3yes4yes

这里的第一个 0 应该被删除并使用数组的索引。我是通过以下声明这样做的:

string = string.replaceFirst(dataLine.substring(0, 1), "");

但是,当我说出这个字符串时:

10yes11no12yes13yes14no

我的代码失败了,因为我想处理10,但我的代码只提取了1

所以在排序中,单个数字可以正常工作,但两位或三位数会导致 IndexOutOfBound 错误。

这是代码:http://pastebin.com/uspYp1FK

这里有一些示例数据:http://pastebin.com/kTQx5WrJ

这是示例数据的输出:

Enter filename: test.txt
Data before cleanUp: {"assignmentID":"2CCYEPLSP75KTVG8PTFALQES19DXRA","workerID":"AGMJL8K9OMU64","start":1359575990087,"end":"","elapsedTime":"","itemIndex":0,"responses":[{"jokeIndex":0,"response":"no"},{"jokeIndex":1,"response":"no"},{"jokeIndex":2,"response":"yes"},{"jokeIndex":3,"response":"yes"},{"jokeIndex":4,"response":"yes"}],"mturk":"yes"},
Data after cleanUp: 0no1no2yes3yes4yes
Data before cleanUp: {"assignmentID":"2118D8J3VE7W013Z4273QCKAGJOYID","workerID":"A2P0GYVEKGM8HF","start":1359576154789,"end":"","elapsedTime":"","itemIndex":3,"responses":[{"jokeIndex":15,"response":"no"},{"jokeIndex":16,"response":"no"},{"jokeIndex":17,"response":"no"},{"jokeIndex":18,"response":"no"},{"jokeIndex":19,"response":"no"}],"mturk":"yes"},
Data after cleanUp: 15no16no17no18no19no
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
    at java.lang.String.substring(String.java:1907)
    at jokes.main(jokes.java:34)

基本上,代码应该做的是将数据剥离为如上所示的字符串,然后读取数字,如果后面跟着yes,则增加它在dataYes中的索引值,或者如果后面跟着no 增加 dataNo 的值。有意义吗?

我能做什么?如何让我的代码更灵活?

【问题讨论】:

  • 你尝试过正则表达式吗?我不熟悉java,但似乎可以用正则表达式来完成。
  • 是否需要删除字符串中的每个数字?
  • 我不太明白你想要什么。您可以在这里发布一些示例输入和输出吗?我打不开那个链接。
  • 你没有很清楚地解释你想要发生的事情。尝试提供有关您希望如何更改输入的更多信息。
  • @idionmarcii:使用正则表达式检查后如何使用数字?

标签: java string


【解决方案1】:

另一种更具体的尝试:-

    String regex = "^(\\d+)(yes|no)";
    String myStr = "10yes11no";

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(myStr);

    while (m.find())
    {
        String all = m.group();
        String digits = m.group(1);
        String bool = m.group(2);

        // do not try and combine the next 2 lines ... it doesn't work!
        myStr = myStr.substring(all.length());
        m.reset(myStr);

        System.out.println(String.format("all = %s, digits = %s, bool = %s", all, digits, bool));
    }

【讨论】:

    【解决方案2】:

    它对你有用吗?

    string = string.replaceAll("^\\d+","");
    

    【讨论】:

    • 据我了解,这将删除所有非数字。但我想解析数字和非数字。
    • @user2027425 据我了解,您的理解是错误的。 ^\\d+ 表示所有前导数字。 ^ 是行首。此代码删除所有前导数字。
    • 我认为这只会删除它们...... OP 是否不想获取数字并同时使用它们?
    • @PeterJamieson 是正确的。我想用这个号码!有没有办法提取前导数字,并将其转换为int 后将其存储在变量中?
    【解决方案3】:

    试试这个

        System.out.println("10yes11no12yes13yes14no".replaceFirst("^\\d+",""));
    

    【讨论】:

      【解决方案4】:

      怎么样:-

      String regex = "^\\d+";
      String myStr = "10abc11def";
      
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(myStr);
      
      if(m.find())
      {
          String digits = m.group();
          myStr = m.replaceFirst("");
      }
      

      【讨论】:

      • 如果您想重新使用匹配器来获取后续数字,也可以从正则表达式中删除 ^。
      • 试过了,导入util.regex.Patternutil.regex.Matcher后也找不到p.group()
      猜你喜欢
      • 2011-06-26
      • 2021-01-29
      • 1970-01-01
      • 2014-01-05
      • 2019-12-04
      • 1970-01-01
      相关资源
      最近更新 更多