Java indexOf 查找字符串中出现的所有单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38620201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
indexOf to find all occurrences of a word in a String
提问by srmjr
I'm trying to use indexOf to find all occurrences of the characters 'the' in a sentence. For example, if the sentence were "The other day I went over there", it should return 3.
我正在尝试使用 indexOf 来查找句子中所有出现的字符“the”。例如,如果句子是“前几天我去那边”,它应该返回 3。
I am able to do this up to the point where it finds the first index, but I'm unsure of how to write the loop. I originally had a for loop that searched the entire string, but it was returning the full string character length, instead of the occurrences of my specified character. How can I write a loop that will find all of the occurrences of the word? Thank you.
我能够做到这一点,直到它找到第一个索引,但我不确定如何编写循环。我最初有一个搜索整个字符串的 for 循环,但它返回的是完整的字符串字符长度,而不是我指定字符的出现次数。如何编写一个循环来查找该单词的所有出现次数?谢谢你。
import java.util.Scanner;
public class TheFinder
{
public static void main (String[] args)
{
String theString = "";
Scanner enter = new Scanner(System.in);
System.out.println("Please enter a sentence: ");
theString = enter.nextLine();
int counter2 = 0;
theString.indexOf("the");
if (theString.indexOf("the")!= -1)
counter2++;
System.out.printf("The characters 'the' were found %d times", counter2);
System.out.println();
System.out.println("This was programmed by -----");
采纳答案by Carlos Afonso
You can keep track of the index:
您可以跟踪索引:
int index = theString.indexOf("the");
while(index >= 0) {
index = theString.indexOf("the", index+1);
counter2++;
}
回答by Bohemian
You have taken a complicated approach. Try this:
你采取了一种复杂的方法。尝试这个:
int count = str.split("the").length - 1;
If you absolutely must use indexOf()
:
如果您绝对必须使用indexOf()
:
str = str.toLowerCase();
int count = 0;
for (int i = str.indexOf("the"); i >= 0; i = str.indexOf("the", i + 1))
count++;
回答by user94559
indexOf
can take a second argumentthat says where to start in the string. So after you find the first occurrence, you can tell indexOf
to only search the string after that index, etc.
indexOf
可以采用第二个参数来说明从字符串中的哪里开始。因此,在找到第一次出现后,您可以告诉indexOf
仅搜索该索引之后的字符串,等等。
This code should work:
此代码应该工作:
public static void main(String[] args) {
String theString = "The other day I went over there.";
theString = theString.toLowerCase();
int index = -1;
int count = 0;
while (true) {
index = theString.indexOf("the", index + 1);
if (index == -1) {
break;
} else {
count += 1;
}
}
System.out.printf("The string 'the' was found %d times.\n", count);
// Output:
// The string 'the' was found 3 times.
}
回答by Chris Gong
The indexOf(String str)
method finds the index of the firstoccurrence of a str
. So if you want to find allthe occurrances of str
then I'd suggest just implementing a loop in which you cut the string once you find an instance of str
and look for str
within theString
again until it is no longer in theString
. Here is what it should look like,
该indexOf(String str)
方法查找a第一次出现的索引str
。所以,如果你想找到所有的occurrancesstr
那么我会建议只是实现一个循环中,你剪一次你发现一个实例的字符串str
,寻找str
内theString
再次,直到它不再theString
。这是它应该是什么样子,
int index = theString.indexOf("the")
int count = 0;
while(index >= 0 && theString.length() > 0){
count++;
theString = theString.substring(0, index + "the".length());
index = theString.indexOf("the");
}
回答by Jonathan Jeffrey
If you want to specifically use indexOf
(I presume this is for an assignment), you can use a recursive method.
如果您想专门使用indexOf
(我认为这是用于赋值),您可以使用递归方法。
public static String numMatches (String str, String query) {
int index = str.indexOf(query);
if (index == -1)
return 0;
else
return 1 + numMatches(str.substring(index + query.length), query);
}
回答by Mathias Stavrou
Maybe you can do something like this, you can use indexOf(String str)
也许你可以做这样的事情,你可以使用 indexOf(String str)
String word = "We are students of the Department of Informatics. All students should know how to program in Java and C.";
int findit = word.indexOf("students");
int count = 0;
for(int i=0; i<=findit; i++)
{
findit = word.indexOf("students" , findit + 1);
count = count + 1;
}
System.out.print(count);
回答by JavaFTW
This checks how many occurrences of a string is inside a string.
这会检查字符串中出现了多少个字符串。
String str = oIn.nextLine();
String st = oIn.nextLine();
int countS = 0;
int index3 = str3.indexOf(st);
while (index >= 0){
index = str.indexOf(st, index+1);
countS++;
}
System.out.println("The number of occurrences of "+st +" in the string " +str3 +" are "+countS);