Search()
{
CString str(_T("he个字符串中含有lp"));int i = str.GetLength();
for(int j=0; j<i; j++)
{
TCHAR h = str.GetAt(j);
if( (h<0XA0B0 && h>122) || h>0xfef7 || (h>90 && h<97) ||(h>57 && h<65) || h<48 }
return FALSE;
}
return TRUE;
}
这个连标点也也排除了,前面要定义_UNICODE
或
#define _UNICODE--------------------
也有其它办法,如下Search(TCHAR* ch) // 这个是你传进来的,要检测的字符串
{
WCHAR* pw = A2W(ch);int i = strlen(ch);
for(int j=0; j<i; j++)
{
WCHAR h = pw[j];
if( (h<0XA0B0 && h>122) || h>0xfef7 || (h>90 && h<97) ||(h>57 && h<65) || h<48 }
return FALSE;
}
return TRUE;
}
1,#define _UNICODE 写在那里!!!
2,我用的是cstring。你改一下程序好不。
顺便问一句。你的能编译出来么!!!
CString是可以转换为TCHAR指针的。
shunruo给你写的函数已经很明了了,汉字为双字节,是利用字符的ASCII码进行比较,应该没有问题,我没有测试过。
我帮你试试改写下,你自己再试试,版权属于shunruo。
BOOL Search(str)
{
//CString str(_T("he个字符串中含有lp"));
TCHAR h;
int i = str.GetLength();
int j =0;
for(j=0; j<i; j++)
{
//TCHAR h = str.GetAt(j);
h= str.GetAt(j);
if( (h<0XA0B0 && h>0x122) || h>0xfef7 || (h>0x90 && h<0x97) ||(h>0x57 && h<0x65) || h<0x48 }
return FALSE;
}
return TRUE;
}
祝你好运!
环境VS 2005:
在项目文件上右击选择属性,在属性中配置属性->常规,在右侧选字符集,选择使用 Unicode 字符集,你试试看。
好像也不用定义_UNICODE 只是上面的代码有一点格式错误,这个应该可以,我试了的:
BOOL Search(CString str) // 这个是你传进来的,要检测的字符串
{ for(int i=0; i<str.GetLength(); i++)
{
WCHAR h = str.GetAt(i);
if( (h<0XA0B0 && h>122) || h>0xfef7 || (h>90 && h<97) ||(h>57 && h<65) || h<48)
return FALSE;
}
return TRUE;
}
for循环中i变量最好在外面定义,还有h,用TCHAR比WCHAR通用,虽然在UNICODE下相同,定义在for循环外好一点。
你的写法会降低执行速度的,如果该函数会被多次执行时就会增大影响了。
for循环中每次比较i的值都需要进行一次str.GetLength()操作。
isdigit()是否数字
isalpha()是否字母
还查到一个博客:http://mailao.yculblog.com/post.1637713.html
int nLength=m_Text.GetLength();
int i=0;
while(i<nLength)
{
char ch=m_Text.GetAt(i);
if( (IsDBCSLeadByteEx(936,ch)) || (IsDBCSLeadByteEx(950,ch)) )
{
i+=2; //简体 or 繁体
}
else
{
i+=1;
}
}它这个函数利用的代码页,应该更准确。
还能判断是简体中文还是繁体中文,有增长了知识,下回应该都看看MSDN了。呵呵,谢谢bobob大侠。
isdigit()是否数字
isalpha()是否字母Remember it.
public class StringFil { public String stringTest() {
String str = "hahahah1234=="; for (int j = 0; j < str.length(); j++) {
char h = str.charAt(j);
if ((h < 0XA0B0 && h > 122) || h > 0xfef7 || (h > 90 && h < 97)
|| (h > 57 && h < 65) || h < 48) {
return "false";
} }
return "true";
} public static void main(String[] args) {
System.out.println(new StringFil().stringTest());
}}
/// <summary>
/// 判断是否为中文,或者英文,或者数字组成的字符串
/// True:是
/// False:不是
/// </summary>
public static bool OnCheckSpecialChar(string p_cStr)
{
string strPat = @"^[a-zA-Z0-9\u4e00-\u9fa5]+$";
Regex objRg = new Regex(strPat);
bool bFlag = objRg.IsMatch(p_cStr);
return bFlag;
}