提取中文字符串

    protected void Page_Load(object sender, EventArgs e)

    {

        string str = "asdf进杂货";

        Response.Write(GetChineseWords(str));

        Response.Write("<br/>");

        foreach (char c in str)

        {

            if (Regex.IsMatch(c.ToString (), "^[\u4E00-\u9FFF]+$"))

                Response.Write(c);

        }

    }

 

    private string GetChineseWords(string str)

    {

        string strRet = string.Empty;

        int start = Convert.ToInt32("4e00", 16);    //范围(0x4e00~0x9fff)

        int end = Convert.ToInt32("9fff", 16);

 

        foreach (char c in str)

        {

            int ci = (int)c;

            if (ci >= start && ci <= end)

            {

                strRet += c;

            }

        }

        return strRet;

    }







4e00~9fff是中文字符在unicode中的范围。

 

你可能感兴趣的:(字符串)