就是在一段字符串中,找出特定字符或字符串出现的位置(他们不止出现一次),
因为不止出现一次,所以用 indexOf()不行,它只能找出首次出现的字符或字符串的位置,因此用 for 也不行,请教大家了,怎么列出它们出现的各个位置,谢谢了。
特定字符或字符串,是输入的一个或一段。
不知道,我说的是否明白,请大家指点一下,谢谢了!!~~~

解决方案 »

  1.   

    Matcher.find()
    Matcher.start()
    Matcher.end()
      

  2.   

    package s1java.sg.chap9;import java.util.Scanner;public class StringBrowser { /**
     * @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);
    }*/
    }}那能帮我改一下吗?
    我还是不太懂啊。
      

  3.   

    Matcher.find()
    Matcher.start()
    Matcher.end()
    都报错,出不来塞
      

  4.   


    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(); 
        } 

    你看看,不知道你是怎么不可以的。能贴出来看看嘛?
      

  5.   

    package test;import java.util.Scanner;public class StringBrowser { /**
     * @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");
    }
    }
    }}
      

  6.   

    这种地方根本不应该使用什么正则表达式,根本是浪费!用indexOf就已经足够了!int index = dest.indexOf(src);
    while(index >= 0) {
        System.out.println("Find at " + index);
        index = dest.indexOf(src, index + 1);
    }
      

  7.   


    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++;
    }
    }
      

  8.   

    RainOnly的看懂了,其实我是不懂正则表达式,呵呵