一个字符串包含另一个[重复]的次数

时间:2021-04-25 23:54:07

Possible Duplicate:
Occurences of substring in a string

可能重复:字符串中子字符串的出现

As in the subject how to check how many times one string contains another one? Example:

如在主题中如何检查一个字符串包含另一个字符串的次数?例:

s1 "babab"
s2 "bab" 
Result : 2

If i use Matcher it does only recognize first occurence:

如果我使用Matcher,它只识别第一次出现:

String s1 = JOptionPane.showInputDialog(" ");
String s2 = JOptionPane.showInputDialog(" ");
Pattern p = Pattern.compile(s2);
Matcher m = p.matcher(s1);
int  counter = 0;
while(m.find()){
    System.out.println(m.group());
    counter++;
}
System.out.println(counter);

I can do it like that, but I would like below to use Java libraries iike Scanner, StringTokenizer, Matcher etc:

我可以这样做,但我想在下面使用Java库iike Scanner,StringTokenizer,Matcher等:

String s1 = JOptionPane.showInputDialog(" ");
String s2 = JOptionPane.showInputDialog(" ");
String pom;
int count = 0;
for(int  i = 0 ; i< s1.length() ; i++){
    if(s1.charAt(i) == s2.charAt(0)){
        if(i + s2.length() <= s1.length()){
            pom = s1.substring(i,i+s2.length());
            if(pom.equals(s2)){
                count++;
            }
        }
    }
 }

 System.out.println(count);

5 个解决方案

#1


4  

One liner solution for the lulz

lulz的一个衬垫解决方案

longStr is the input string. findStr is the string to search for. No assumption, except that longStr and findStr must not be null and findStr must have at least 1 character.

longStr是输入字符串。 findStr是要搜索的字符串。没有假设,除了longStr和findStr不能为null并且findStr必须至少有1个字符。

longStr.length() - longStr.replaceAll(Pattern.quote(findStr.substring(0,1)) + "(?=" + Pattern.quote(findStr.substring(1)) + ")", "").length()

Since 2 matches are considered different as long as they starts at different index, and overlapping can happen, we need a way to differentiate between the matches and allow for matched part to be overlapped.

由于2个匹配被认为是不同的,只要它们以不同的索引开始,并且可能发生重叠,我们需要一种方法来区分匹配并允许匹配的部分重叠。

The trick is to consume only the first character of the search string, and use look-ahead to assert the rest of the search string. This allows overlapping portion to be rematched, and by removing the first character of the match, we can count the number of matches.

诀窍是只使用搜索字符串的第一个字符,并使用前瞻来断言搜索字符串的其余部分。这允许重叠部分被重新匹配,并且通过移除匹配的第一个字符,我们可以计算匹配的数量。

#2


2  

i think this might work if you know the word you are looking for in the string you might need to edit the regex pattern tho.

我想如果你知道你在编辑正则表达式模式的字符串中找到的单词,这可能会有用。

String string = "hellohellohellohellohellohello";
Pattern pattern = Pattern.compile("hello"); 
Matcher matcher = pattern.matcher(string);
int count = 0;
while (matcher.find()) count++;

#3


1  

The class Matcher has two methods "start" and "end" which return the start index and end index of the last match. Further, the method find has an optional parameter "start" at which it starts searching.

Matcher类有两个方法“start”和“end”,它们返回最后一个匹配的起始索引和结束索引。此外,方法find具有可选参数“start”,在该参数处开始搜索。

#4


0  

Some quick Bruce Forte solution:

一些快速的Bruce Forte解决方案:

    String someString = "bababab";
    String toLookFor = "bab";
    int count = 0;
    for (int i = 0; i < someString.length(); i++) {
        if (someString.length() - i >= toLookFor.length()) {
            if (someString.substring(i, i + toLookFor.length()).equals(toLookFor) && !"".equals(toLookFor)) {
                count++;
            }
        }
    }
    System.out.println(count);

This prints out 3. Please note I assume that none of the Strings is null.

这打印出3.请注意我假设没有任何字符串为空。

#5


0  

you can do it like this

你可以这样做

private int counterString(String s,String search) {
    int times = 0;
    int index = s.indexOf(search,0);
    while(index > 0) {
        index = s.indexOf(search,index+1);
        ++times;
    }
    return times;
 }

#1


4  

One liner solution for the lulz

lulz的一个衬垫解决方案

longStr is the input string. findStr is the string to search for. No assumption, except that longStr and findStr must not be null and findStr must have at least 1 character.

longStr是输入字符串。 findStr是要搜索的字符串。没有假设,除了longStr和findStr不能为null并且findStr必须至少有1个字符。

longStr.length() - longStr.replaceAll(Pattern.quote(findStr.substring(0,1)) + "(?=" + Pattern.quote(findStr.substring(1)) + ")", "").length()

Since 2 matches are considered different as long as they starts at different index, and overlapping can happen, we need a way to differentiate between the matches and allow for matched part to be overlapped.

由于2个匹配被认为是不同的,只要它们以不同的索引开始,并且可能发生重叠,我们需要一种方法来区分匹配并允许匹配的部分重叠。

The trick is to consume only the first character of the search string, and use look-ahead to assert the rest of the search string. This allows overlapping portion to be rematched, and by removing the first character of the match, we can count the number of matches.

诀窍是只使用搜索字符串的第一个字符,并使用前瞻来断言搜索字符串的其余部分。这允许重叠部分被重新匹配,并且通过移除匹配的第一个字符,我们可以计算匹配的数量。

#2


2  

i think this might work if you know the word you are looking for in the string you might need to edit the regex pattern tho.

我想如果你知道你在编辑正则表达式模式的字符串中找到的单词,这可能会有用。

String string = "hellohellohellohellohellohello";
Pattern pattern = Pattern.compile("hello"); 
Matcher matcher = pattern.matcher(string);
int count = 0;
while (matcher.find()) count++;

#3


1  

The class Matcher has two methods "start" and "end" which return the start index and end index of the last match. Further, the method find has an optional parameter "start" at which it starts searching.

Matcher类有两个方法“start”和“end”,它们返回最后一个匹配的起始索引和结束索引。此外,方法find具有可选参数“start”,在该参数处开始搜索。

#4


0  

Some quick Bruce Forte solution:

一些快速的Bruce Forte解决方案:

    String someString = "bababab";
    String toLookFor = "bab";
    int count = 0;
    for (int i = 0; i < someString.length(); i++) {
        if (someString.length() - i >= toLookFor.length()) {
            if (someString.substring(i, i + toLookFor.length()).equals(toLookFor) && !"".equals(toLookFor)) {
                count++;
            }
        }
    }
    System.out.println(count);

This prints out 3. Please note I assume that none of the Strings is null.

这打印出3.请注意我假设没有任何字符串为空。

#5


0  

you can do it like this

你可以这样做

private int counterString(String s,String search) {
    int times = 0;
    int index = s.indexOf(search,0);
    while(index > 0) {
        index = s.indexOf(search,index+1);
        ++times;
    }
    return times;
 }