现在的位置: 首页 > 综合 > 正文

检测字符串中是否有中文

2012年06月03日 ⁄ 综合 ⁄ 共 659字 ⁄ 字号 评论关闭

[DllImport("KERNEL32.DLL", SetLastError=true,  
        CharSet=CharSet.Unicode, ExactSpelling=true,  
        CallingConvention=CallingConvention.StdCall)]  
private static extern int WideCharToMultiByte(
    uint    CodePage,
        uint    dwFlags,
        string  lpWideCharStr,
    int     cchWideChar, 
    string  lpMultiByteStr,
    int     cchMultiByte,
        string  lpDefaultChar, 
    int     lpUsedDefaultChar);

private const uint CP_ACP=0x0;

public void IsHaveChinese()
{
    string m_str="中ABC言语";
    int nLen=WideCharToMultiByte(CP_ACP,0,m_str,m_str.Length,null,0,null,0);
    if (m_str.Length != nLen)
    {
        MessageBox.Show("有中文字符。");
    }
    else
    {
        MessageBox.Show("没有中文字符。");
    }
}

抱歉!评论已关闭.