如何判断一个字符串只是由字母和数字组成,而不含有特殊符号以及汉字等其他非法字符?

解决方案 »

  1.   

    bool b=true;
    for(int i=0;i<cstring.GetLength()&&b;i++)
    {
    char c = cstring.GetAt(i);
    if((c>='0'&&c<='9')||(c>='A'&&c<='Z')||(c>='a'&&c<='z'))
      b=true;
    else
     b=false;}
      

  2.   

    楼上的方法麻烦了,
    在<cctype>头文件里有一个函数bool isalnum(char c)专门判断一个字符是否是字母或数字
      

  3.   

    //现汉字采用GB18030编码
    CString str="123中a华bz,1。2;3人2喆*";//喆
    CString changestr;
    changestr="";
    CString show;
    CString strTemp;
    show="";
    int len = str.GetLength();
    for(int i = 0;i<len;i++)
    {
    strTemp.Format("%02X,",(unsigned char)str[i]);
    show+=strTemp;
    if(((unsigned char)(str.GetAt(i))>0x81) && ((unsigned char)(str.GetAt(i))<0xFE) )//如果属于汉字;str.GetAt(i))>0xaf && (str.GetAt(i)<0xfe)喆
    {
    changestr+=str.Mid(i,2);
    i++;
    strTemp.Format("%02X,",(unsigned char)str.GetAt(i));
    show+=strTemp;
    }
    else{
    //AfxMessageBox("不是汉字!");
    }
    }
    str+="\r\n";
    show=str+show;
    show+="\r\n";
    show+=changestr;
    //AfxMessageBox(show);
    AfxMessageBox(str+changestr);
      

  4.   

    bool b=true;for(int i=0;i<cstring.GetLength()&&b;i++)
    {
    char c = cstring.GetAt(i);
    if(!((c>='0'&&c<='9')||(c>='A'&&c<='Z')||(c>='a'&&c<='z')))
    { b=false;
       break;
    }}