Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 一个字符串包含另一个字符串的次数_Java_Regex_String - Fatal编程技术网

Java 一个字符串包含另一个字符串的次数

Java 一个字符串包含另一个字符串的次数,java,regex,string,Java,Regex,String,可能重复: 在主题中,如何检查一个字符串包含另一个字符串的次数? 例如: 如果我使用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()){

可能重复:

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

如果我使用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);
我可以这样做,但是我想在下面使用Java库,如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);
String s1=JOptionPane.showInputDialog(“”);
字符串s2=JOptionPane.showInputDialog(“”);
弦聚甲醛;
整数计数=0;
对于(int i=0;i如果(i+s2.length()某种快速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);
String someString=“bababab”;
字符串toLookFor=“bab”;
整数计数=0;
for(int i=0;i=toLookFor.length()){
if(someString.substring(i,i+toLookFor.length()).equals(toLookFor)和&&!“”.equals(toLookFor)){
计数++;
}
}
}
系统输出打印项次(计数);

这将打印出3。请注意我假设
字符串
s中没有一个为空。

该类有两个方法“”和“”,它们返回最后一个匹配的开始索引和结束索引。此外,该方法还有一个可选参数“start”它开始搜索的时候。

我认为如果您知道字符串中要查找的单词,则可能需要编辑正则表达式模式tho

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

你可以这样做

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

针对lulz的一种线性解决方案

longStr
是输入字符串。
findStr
是要搜索的字符串。除了
longStr
findStr
必须not
null
findStr
必须至少有一个字符外,不作任何假设

longStr.length() - longStr.replaceAll(Pattern.quote(findStr.substring(0,1)) + "(?=" + Pattern.quote(findStr.substring(1)) + ")", "").length()
由于两个匹配被认为是不同的,只要它们从不同的索引开始,并且重叠可能发生,因此我们需要一种方法来区分匹配,并允许匹配部分重叠


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

这是家庭作业吗?您可以简单地使用
string#indexOf()
在一个while循环中,从上次找到的索引开始。对于字符串
babab
和模式
bab
将计数设为
1
。您能为“bababab”运行它并查看结果吗?如果它是两个,则模式会丢弃它已经找到的字符串部分。我认为它所做的是“babab ab”有一个,所以这可能不是最适合你的..也许这会更好(没有测试过并写在记事本上,所以要小心:D)String text=“babab”String matchWord=“bab”String newWord=“”;char[]chars=text.split(“”);int counter;for(int j=0;j这是如何工作的?我不理解Pattern.quote方法什么是literal regex?@user1915933:这意味着即使字符串包含可以识别为regex的字符或序列,这些字符或序列也将被中和为普通字符。
longStr.length() - longStr.replaceAll(Pattern.quote(findStr.substring(0,1)) + "(?=" + Pattern.quote(findStr.substring(1)) + ")", "").length()