简体   繁体   English

java中的str.find()等价物?

[英]str.find() equivalent in java?

Hi I'm trying to learn java so I'm converting my working c++ code to java.嗨,我正在尝试学习 Java,所以我正在将我的工作 C++ 代码转换为 Java。 But I'm unsure about how to proceed with str.find().但我不确定如何继续使用 str.find()。 Is there an equivalent of this in java?在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.我知道 indexOf() 和 indexLastOf 但它们只会产生第一次和最后一次出现。

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); 您可以使用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. 它会在第二个参数的int之后找到“ some string”的第一个匹配项,并返回一个int。

Try using Java's Pattern and Matcher. 尝试使用Java的模式和匹配器。 Those classes involve regex pattern matching. 这些类涉及正则表达式模式匹配。 It is very easy. 这很容易。 Search here for other existing posts on that subject. 在此处搜索有关该主题的其他现有帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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