一些字符串如“123 是发达大”、“215 结合客户交互和好”其中汉字多少不定,有类似substring()之类的函数吗,想把里面的汉字部分全部提取出来?

解决方案 »

  1.   

    3 是发达大123有个sadf
    这样子要提出哪些汉字
      

  2.   

    如果你的字符串是固定格式: 数值 + 空格 + 汉字 的话,可以用正则表达式:
    String exp = "123 是发达大";
    String result = exp.replace("\\d*\\s*","");
      

  3.   

    1. 可以用正则表达式匹配中文 [\u4e00-\u9fa5]
    2. 字符对应的编码值在,19968和40869之间的为中文   
    string name = "nihao高峰";
                int currentcode=-1;
                for (int i = 0; i < name.Length; i++)
                {
                    currentcode=(int)name[i];
                    if (currentcode >19968&¤tcode < 40869)
                    {
                        Console.WriteLine(name[i].ToString());
                    }
                } 类似这样去拿就可以解决问题了 
      

  4.   

    System.Text.RegularExpressions.MatchCollection mc= System.Text.RegularExpressions.Regex.Matches("123 是发达大 215 结合客户交互和好", @"[\u4e00-\u9fa5]");
    System.Collections.IEnumerator list= mc.GetEnumerator();
    string result="";
    while(list.MoveNext())
        result+=list.Current.ToString();
      

  5.   

    去掉所有ascii字符剩下就是汉字
      

  6.   

    我这个用java写的,给你参考一下。import java.io.*;
    //先在编译的当前文件夹下建一个文本,里面写汉字,
    //文件名为a.txt。
    public class ReadTest{
    public static void main(String[] args){
      DataInputStream din=null;
      try{
      //定义一个输入流,把当前文件夹下的a.txt里的内容
      //以GB2312字符集读出
      InputStreamReader is=new InputStreamReader(new FileInputStream("a.txt"),"GB2312");
    String str1="";
    String str2="";
    //循环度每一个字符,并连接在一个字符串str1上
    int i=-1;
    while((i=is.read())!=-1)
     {
      char m=(char)i;
      str2=str2.valueOf(m);
      str1=str1+str2;
    }
    //对字符串给出一些简单的操作
    str1=str1.replaceAll(","," ");
    //把所有逗号替换为空格
    str1=str1.replaceAll("。"," ");
    //把所有句号替换为空格
    System.out.println(str1+"!"); //打印输出
     }catch(FileNotFoundException e)
    {//异常捕获
    e.printStackTrace();
    }
      catch(IOException e){
      e.printStackTrace();
      }
    finally{    //最终行为---关闭流~~~
    try{
         if(din !=null){
          din.close();
         }
    }catch(IOException e){
      e.printStackTrace();
          }
        }
      }
    }
      

  7.   


        /// <summary>
        /// 返回字符串中的汉字
        /// </summary>
        /// <param name="str">原字符串</param>
    private string getTemp(string str)
        {
            char[] clist= str.ToCharArray();
            string tmpstr = "";
            int code = 0;
            int chfrom = Convert.ToInt32("4e00", 16);    //范围(0x4e00~0x9fff)转换成int(chfrom~chend)
            int chend = Convert.ToInt32("9fff", 16);
            for(int i=0;i<clist.Length;i++)
            {
                code =Char.ConvertToUtf32(str,i);
                if (code >= chfrom && code <= chend)     
                {
                    tmpstr += clist[i].ToString();
                }        }
            return tmpstr;
        }
      

  8.   

    string str="123 是发达大 215 结合客户交互和好 12131sfagsa";
    string ret=System.Text.RegularExpressions.Regex.Replace(str,@"[^(\u4e00-\u9fa5)]","");
      

  9.   

    除了ascii字符以外,不是汉字的字符多着呢,比如拉丁文,俄文等等