Java 在字符串中查找子字符串的位置(不是 indexOf)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18844855/
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
Find position of a substring in a string(not indexOf)
提问by user2786737
ex
前任
String str = "abc def ghi";
Is there any method like str.find("abc")
will return me 1
and str.find("def")
will return me 2?
有什么方法str.find("abc")
可以返回我 1 并str.find("def")
返回我 2 吗?
java language..
java语言..
采纳答案by Bernhard Barker
How about something like this?
这样的事情怎么样?
int getIndex(String str, String substring)
{
return Arrays.asList(str.split("\s+")).indexOf(substring)+1;
}
Disclaimer:This isn't at all efficient. It splits the whole string from scratch every time the function is called.
免责声明:这根本没有效率。每次调用该函数时,它都会从头开始拆分整个字符串。
Test code:
测试代码:
String str = "abc def ghi";
System.out.println(getIndex(str, "abc"));
System.out.println(getIndex(str, "def"));
Prints:
印刷:
1
2
Explanation:
解释:
str.split("\\s+")
splits the string by white-space and puts each part into a position in an array.
str.split("\\s+")
按空格分割字符串并将每个部分放入数组中的一个位置。
Arrays.asList
returns an ArrayList
for the array.
Arrays.asList
返回ArrayList
数组的 。
indexOf(substring)
finds the position of the string in the ArrayList
.
indexOf(substring)
查找字符串在 中的位置ArrayList
。
+1
since Java uses 0-indexing and you want 1-indexing.
+1
因为 Java 使用 0-indexing 而你想要 1-indexing。
回答by MansoorShaikh
Since you are not requesting for index of a substring but instead which word position the substring belong to, there is no such built-in method available. But you can split the input string by space character and read each item in the list returned by the split method and check at which list item position your substring belongs to.
由于您不是请求子字符串的索引,而是请求子字符串所属的单词位置,因此没有可用的内置方法。但是您可以按空格字符拆分输入字符串并读取 split 方法返回的列表中的每个项目并检查您的子字符串所属的列表项目位置。
Let me know, if you need code for this.
如果您需要代码,请告诉我。
回答by Callum
I don't think there is a native function for this. But you could write your own.
我不认为有一个本机功能。但是你可以自己写。
It seems like you want to split the string based on the white space character.
似乎您想根据空格字符拆分字符串。
String[] parts = string.split(" ");
Loop through the array created. And return the index + 1 (As java has zero based indexes)
循环遍历创建的数组。并返回索引 + 1(因为 java 有基于零的索引)
for(int i = 0; i < parts.length; i++)
{
if(parts[i].equals(parameter))
{
return i + 1;
}
}
回答by Zachariah Rabatah
If you are looking to find multiple positions of the same string try this code.
如果您要查找同一字符串的多个位置,请尝试使用此代码。
//Returns an array of integers with each string position
private int[] getStringPositions(String string, String subString){
String[] splitString = string.split(subString); //Split the string
int totalLen = 0; //Total length of the string, added in the loop
int[] indexValues = new int[splitString.length - 1]; //Instances of subString in string
//Loop through the splitString array to get each position
for (int i = 0; i < splitString.length - 1; i++){
if (i == 0) {
//Only add the first splitString length because subString is cut here.
totalLen = totalLen + splitString[i].length();
}
else{
//Add totalLen & splitString len and subStringLen
totalLen = totalLen + splitString[i].length() + subString.length();
}
indexValues[i] = totalLen; //Set indexValue at current i
}
return indexValues;
}
So for example:
例如:
String string = "s an s an s an s"; //Length = 15
String subString = "an";
Answer would return indexValues = (2, 7, 12).
答案将返回 indexValues = (2, 7, 12)。