Java 返回字符串的特定子字符串的第一个字母的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19025654/
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
return index of the first letter of a specific substring of a string
提问by AmanArora
I am given a string and I wish to find out the index of the first character of a substring of that string whenever it appears the very first time in the parent string.
我得到了一个字符串,我希望在它第一次出现在父字符串中时找出该字符串的子字符串的第一个字符的索引。
For example, given a string "itiswhatitis" and the substring "is", the output should be 2.
例如,给定字符串“itiswhatitis”和子字符串“is”,输出应为 2。
采纳答案by Prabhakaran Ramaswamy
You can use as below
您可以使用如下
int indexOf(String str)
from your example string.
从您的示例字符串。
String s ="itiswhatitis";
int index = s.indexOf("is");
回答by Mohayemin
"itiswhatitis".indexOf("is")
Try this
尝试这个
回答by sunysen
public static void main(String[] args) {
int index = "itiswhatitis".indexOf("is");
System.out.println(index);
}
回答by Balaji Dhanasekar
String str="itiswhatitis";
int index=str.indexOf("is");