我有一个字符串,我想使用一些公式,其中有一个单词将被搜索。有时这个词可能位于不同的位置,具体取决于用户。为了使子串工作,我必须确保当它发生时,代码仍然可以工作。
String temp = s;//Temp is a long string text
String fehlerspeicher="fehlerspeicher";
if(temp.matches(".*"+fehlerspeicher+".*")){
// i have to find as an integer, how many LETTERS are used till this spesific word
}//to make changes in the following code
String temp1=temp.substring(0, 15000);//15000 is an example. it can be 5000 or 20000 sometimes. It splits the text up from 15000th letter.
String temp2=temp.substring(15000);// It'd be useful to use this integer in these 2 formulas.
temp2=temp2.replaceFirst("200", "20_");
temp=temp1+temp2;
那么,它可以以某种方式实现吗?感谢。
答案 0 :(得分:2)
使用indexOf:
public int indexOf(String str)
返回第一次出现的字符串中的索引 指定子字符串。
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#indexOf(java.lang.String)
所以要获得“fehlerspeicher”的索引,你可以使用:
int index = temp.indexOf("fehlerspeicher");
所以要在'fehlerspeicher'中获得'f'的位置,你可以使用:
String temp = "hellofehlerspecher"; //example temp string
int index = temp.indexOf("fehlerspeicher");
int fPos = index + 1; //fpos will equal 6.