[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("没有中文字符。");
}
}
博客展示了一段C#代码,通过调用KERNEL32.DLL中的WideCharToMultiByte函数,将宽字符字符串转换为多字节字符串,根据转换前后字符串长度是否一致,来判断字符串中是否包含中文字符。

被折叠的 条评论
为什么被折叠?



