如内容 abcdadde 我要得到a在内容里的各个位置,请问如何做啊?

解决方案 »

  1.   

    <script>
    var src = " abcdadde";
    var re = /a/gi;
    var arr;
    while ((arr = re.exec(src)) != null)
    {
    document.write(arr.index + " - "+ arr.lastIndex + " : " + arr + "<br>");
    }
    </script>
      

  2.   

     string strIndex1 = "";
                string strIndex2 = "";
                //Regex reg = new Regex("\b"+strChar+"\b");
                Regex reg = new Regex(strChar);
                Match match = reg.Match(s);
                while(match.Success)
                {
                    strIndex1 += s.LastIndexOf(match.Groups[0].ToString()) + strChar;
                    strIndex2 += s.IndexOf(match.Groups[0].ToString()) + strChar;
                }
                return strIndex1;不行
      

  3.   

    //字符所在位置
    int b=Form1.str.IndexOf("a",0,str.Length);//字符"a"在字符串中第一次出现的位置
    int x=b+1;
    while(true)
    {
    int c=Form1.str.IndexOf("a",x,str.Length-x);//判断下一个查找的字符的位置。
    if(c==-1)//如果该字符(a)在剩下的字符串中不存在
    {
    break;
    }
    x=b+1+c;
    if(x>str.Length)//当所有字符串都搜索完毕
    {
    break;
    }
    }