简体   繁体   中英

How do I read and remove a number from a string?

So for example, I have this string:

0no1no2yes3yes4yes

The first 0 here should be removed and used an an index of array. I am doing so by this statement:

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

However, when I have say this string:

10yes11no12yes13yes14no

My code fails, since I want to process the 10 but my code extracts just the 1 .

So in sort, single digits work fine, but double or triple digits cause IndexOutOfBound Error.

Here's the code: http://pastebin.com/uspYp1FK

And here's some sample data: http://pastebin.com/kTQx5WrJ

Here's the output for the sample data:

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)

Basically, what the code is supposed to do is strip off the data into strings as shown above, and then read the number, and if it's followed by yes increase it's index's value in dataYes , or if followed by no increase value in dataNo . Makes sense?

What can I do? How can I make my code more flexible?

对你起作用吗?

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

How about: -

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("");
}

尝试这个

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

An alternative, more specific attempt: -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM