JAVA字符串

来源:百度知道 编辑:UC知道 时间:2024/05/11 00:22:13
输入一段字符,例如:我的妈啊!哪位大哥帮帮我解决这个问题,小弟我在此谢过!
输入要查询的字符串:我
“我”出现的位置是:0 11 21
请问要达到这个效果应该用JAVA怎么做?帮帮忙!

用String.indexOf ()函数实现。

String s = "我的妈啊!哪位大哥帮帮我解决这个问题,小弟我在此谢过!" ;

int index = 0 ;

while ((index = s.indexOf ('我', index))>= 0)
{
System.out.println (index) ;
index ++ ;
}

public class IndexTest {
public static void main(String args[]){
String str="我的妈啊!哪位大哥帮帮我解决这个问题,小弟我在此谢过!";
List<Integer> list=locate(str);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
public static List<Integer> locate(String str){
List<Integer> list=new ArrayList<Integer>();
int location;
int len=0;
while((location=str.indexOf("我"))!=-1){
str=str.substring(location+1);
list.add(new Integer(location+len));
len+=location+1;
}
return list;
}
}

看是不是需要这种效果

public class FindWord {
public static void main(String[] ar