简体   繁体   中英

str.find() equivalent in java?

Hi I'm trying to learn java so I'm converting my working c++ code to java. But I'm unsure about how to proceed with str.find(). Is there an equivalent of this in java? What I want to happen is I look for the first occurrence of a string in another string after the first comma not integer. I know of indexOf() and indexLastOf but they will only produce the fist and last occurrences.

void found() {
    int count = 1;
    size_t find = str.find(','); 
        while (find != string::npos) {
            count++;
            find = str.find(',', find + 1);
        }
     }
};

you can use str.indexOf('some string', indexToStartLooking); which will find the first occurrence of "some string" after the int in the second parameter and will return an int.

Try using Java's Pattern and Matcher. Those classes involve regex pattern matching. It is very easy. Search here for other existing posts on that subject.

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