就是在一段字符串中,找出特定字符或字符串出现的位置(他们不止出现一次),
因为不止出现一次,所以用 indexOf()不行,它只能找出首次出现的字符或字符串的位置,因此用 for 也不行,请教大家了,怎么列出它们出现的各个位置,谢谢了。
特定字符或字符串,是输入的一个或一段。
不知道,我说的是否明白,请大家指点一下,谢谢了!!~~~
因为不止出现一次,所以用 indexOf()不行,它只能找出首次出现的字符或字符串的位置,因此用 for 也不行,请教大家了,怎么列出它们出现的各个位置,谢谢了。
特定字符或字符串,是输入的一个或一段。
不知道,我说的是否明白,请大家指点一下,谢谢了!!~~~
Matcher.start()
Matcher.end()
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("请输入一段字符串:");
String inStr = input.next();
System.out.print("\n请输入要查找的字符串:");
String serStr = input.next();
System.out.print("我出现的位置是:");
int site = inStr.indexOf(serStr) + 1;
int i = 0;
while (i < inStr.length()) {
System.out.print(site);
i++;
}
//System.out.print("\t" + site);
/*for (int i = 0; i < inStr.length(); i++) {
System.out.print("\t" + site);
}*/
}}那能帮我改一下吗?
我还是不太懂啊。
Matcher.start()
Matcher.end()
都报错,出不来塞
import java.util.regex.*; public class RegularExpressionDemo2 {
public static void main(String[] args) {
String text = "abcdebcadxbc"; Pattern pattern = Pattern.compile(".bc");
Matcher matcher = pattern.matcher(text); while(matcher.find()) {
System.out.println(matcher.group());
}
System.out.println();
}
}
你看看,不知道你是怎么不可以的。能贴出来看看嘛?
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("请输入一段字符串:");
String inStr = input.next(); System.out.print("\n请输入要查找的字符串:");
String serStr = input.next(); System.out.print("我出现的位置是:");
int index = 0;
for(int i = 0; i < inStr.length(); i ++){
//遍历到字符串末尾,要查找的字符长度大于末尾字符长度,则退出循环遍历
if(i + serStr.length() > inStr.length()){
break;
}
if(inStr.substring(i, i + serStr.length()).equals(serStr)){
index = i+1;
System.out.print(index + "\t");
}
}
}}
while(index >= 0) {
System.out.println("Find at " + index);
index = dest.indexOf(src, index + 1);
}
public static void main(String[] args) {
find("dfsafdasfdsaffdsaffd","sa");
}
private static void find(String source,String find_str){
int index = 0;
for (int i = find_str.length()-1; i < source.length(); i++) {
if(source.substring(index,i+1).equals(find_str)){
System.out.println(index);
}
index++;
}
}