1.正则表达式?
2.程序判断连续的几个字符是否是字母?
想到这二种方式,还有什么更好的办法?

解决方案 »

  1.   

      string str =@"void Application_Start(object sender, EventArgs e) 
        {
            // 在应用程序启动时运行的代码
            Application[""total""] = 500000;
            Application[""online""] = 2500;
        }";            System.Text.RegularExpressions.Regex   reg = new Regex("([a-zA-Z]+)");
                System.Text.RegularExpressions.MatchCollection mc = reg.Matches(str);
                foreach (Match m in mc)
                {
                    Response.Write(m.Result("$1") + "<BR>");
                }
      

  2.   

    文本有可能比较大,几十KB的样子
    大概格式:
    814
    00:42:23,100 --> 00:42:26,400
    But it comes with sacrifice, believe me.815
    00:42:27,400 --> 00:42:28,900
    And it's time for you to realize816
    00:42:28,900 --> 00:42:34,300
    that that talent- it doesn't belong to just you anymore.817
    00:42:55,800 --> 00:43:01,800
    <font color="#4096d1">破烂熊字幕组
    -==http://www.ragbear.com==-
    欢迎加入</font>1
    00:00:01,800 --> 00:00:04,800
    <font color=#4096d1>以下故事内容纯属虚构 如有雷同 实属巧合</font>2
    00:00:49,200 --> 00:00:50,600
    嗨 Walid3
    00:00:50,600 --> 00:00:52,400
    你好
      

  3.   

    ([a-zA-Z]+)这就是匹配连续的一个或者多个单词的作用吧?
      

  4.   


    几十kb根本算不上大,正则完全可以处理,效率上也不会与其它方法有明显的区别另外英文单词中,如果“-”和“'”不出现在首尾,应该算是英文单词的一部分,这个要看需求了Regex reg = new Regex(@"(?i)\b(?!-)[a-z'-]+(?<!-)\b");
    MatchCollection mc = reg.Matches(yourStr);
    foreach (Match m in mc)
    {
        richTextBox2.Text += m.Value + "\n";
    }
      

  5.   

    哦,漏了“'”的判断
    Regex reg = new Regex(@"(?i)\b(?!['-])[a-z'-]+(?<!['-])\b");
    MatchCollection mc = reg.Matches(yourStr);
    foreach (Match m in mc)
    {
        richTextBox2.Text += m.Value + "\n";
    }不过严格来说,有些情况下“'”出现在尾部也是单词的一部分,要看是否有进行判断的必要了
      

  6.   


    大小写不同算重复吗?List<string> list = new List<string>();
    Regex reg = new Regex(@"(?i)\b(?!['-])[a-z'-]+(?<!['-])\b");
    reg.Replace(yourStr, delegate(Match m) { if (!list.Contains(m.Value)) list.Add(m.Value); return ""; });
    foreach (string s in list)
    {
        richTextBox2.Text += s + "\n";
    }以上未做大小写的判断,有需要自己加下就是了
      

  7.   

    如果其中出现"hello hello hello"这样的情况,得到的Collection中会有3个hello
    如何避免呢?
      

  8.   

    你写个泛型数组LIST<STRING>存放,如果CONTAINS则不添加,再输出成字符串
      

  9.   

    用类型也可以
    for(int i = 0; i < length ; i++)
    {
        charType[i] = Char.GetUnicodeCategory(str,i).ToString();
    }
    charType[i]== "LowercaseLetter"//OtherLetter 汉字
    //LowercaseLetter 字母
    //SpaceSeparator 空格
    //OtherPunctuation 英文逗号
    //OtherPunctuation 中文逗号
      

  10.   


    10楼代码中两处m.Value都改为m.Value.ToLower()就可以了