我想要写一个字符串浏览器:
例如:
输入字符串1:120120120
要查询的字符串2:0
输出<字符串2>在<字符串1>中的位置:2、6、8
可是我写不下去了,没办法用循环做出来。有没有高手,能在我的基础上,完成。感谢!!import java.util.*;public class StringExplorer {
// 字符串浏览器
String word;
String wordSearch; public void show() {
Scanner s = new Scanner(System.in);
System.out.println("请输入一段字符:");
word = s.next();
System.out.println("请输入要查询的字符串:");
wordSearch = s.next(); int index = word.indexOf(wordSearch);
System.out.println(index);//输出要的查询的字符串的位置
}}
public class test_explorer { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StringExplorer StringExplorer_obj=new StringExplorer();
StringExplorer_obj.show();
}}
例如:
输入字符串1:120120120
要查询的字符串2:0
输出<字符串2>在<字符串1>中的位置:2、6、8
可是我写不下去了,没办法用循环做出来。有没有高手,能在我的基础上,完成。感谢!!import java.util.*;public class StringExplorer {
// 字符串浏览器
String word;
String wordSearch; public void show() {
Scanner s = new Scanner(System.in);
System.out.println("请输入一段字符:");
word = s.next();
System.out.println("请输入要查询的字符串:");
wordSearch = s.next(); int index = word.indexOf(wordSearch);
System.out.println(index);//输出要的查询的字符串的位置
}}
public class test_explorer { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StringExplorer StringExplorer_obj=new StringExplorer();
StringExplorer_obj.show();
}}
// 字符串浏览器
String word;
String wordSearch; public void show() {
Scanner s = new Scanner(System.in);
System.out.println("请输入一段字符:");
word = s.next();
System.out.println("请输入要查询的字符串:");
wordSearch = s.next();
int index = 0;
int i = 0;
do {
index = word.indexOf(wordSearch, i);//wordSearch是要查找的字符,i是
if (index == -1) {
break;
}
i = wordSearch.length() + index;// 加上wordSearch的字符长度后在,在进行查找
System.out.println("字符" + wordSearch + "出现的位置:");
System.out.print(index + " ");// 输出要的查询的字符串的位置
} while (index < word.length());
}}public class test_explorer { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StringExplorer StringExplorer_obj = new StringExplorer();
StringExplorer_obj.show();
}}