byte[] temp=str.getBytes();
for (int i=0;i<temp.length;i++){
if(temp[i]>=0)
return false;
}
return true;
如果str中含有非中文字符,返回false
for (int i=0;i<temp.length;i++){
if(temp[i]>=0)
return false;
}
return true;
如果str中含有非中文字符,返回false
中文字符在0x4E00和0x9FFF之间,中文标点在0xFF00之后
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
using namespace std;class CHz2Py
{
public:
CHz2Py()
{
m_bLowerCase = false;
m_bReplaceHzNumber = false;
} // change a single ch to en char getHzPy(const char* hz)
{
static const WORD qwmb[] = {
0xB0A1, 0xB0C5, 0xB2C1, 0xB4EE, 0xB6EA, 0xB7A2, 0xB8C1, 0xB9FE, 0xBBF7,
0xBFA6, 0xC0AC, 0xC2E8, 0xC4C3, 0xC5B6, 0xC5BE, 0xC6DA, 0xC8BB, 0xC8F6,
0xCBFA, 0xCDDA, 0xCEF4, 0xD1B9, 0xD4D1, 0xD7F9
};
static const char caption[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'W', 'X', 'Y', 'Z'
};
m_bHz = false;
WORD qwm = MAKEWORD(hz[1], hz[0]); // out of rangel
if (qwm > qwmb[23])
return hz[0]; // chinese symblos
if (m_bReplaceHzNumber)
{
if (qwm >= 0xA3A1 && qwm <= 0xA3FD)
{
m_bHz = true;
return qwm - 0xA3A1 + '!';
}
} // not a chinese
if ((BYTE)hz[0] < 0xB0)
return hz[0];
for (int i = 0; qwm >= qwmb[i + 1]; i++);
m_bHz = true;
return m_bLowerCase ? caption[i] + 32 : caption[i];
}
// change a string of chinese to english char* Hz2Py(const char* pHz, char* pBuf, int* len = NULL)
{
// with pBuf NULL can get the length of buf in len
if (pBuf == NULL)
{
if (len != NULL)
*len = (strlen(pHz) + 1);
return NULL;
}
const char* pIn = pHz;
char* pOut = pBuf;
while (*pIn != 0)
{
*pOut = getHzPy(pIn);
if (m_bHz)
pIn += 2;
else
pIn++;
pOut++;
}
*pOut = 0;
return pBuf;
}
bool m_bLowerCase; // return with lower case
bool m_bReplaceHzNumber; // change the chinse symbols to englist symbols
private:
bool m_bHz;};int main()
{
char Buf[MAX_PATH];
CHz2Py Hz2Py;
Hz2Py.m_bLowerCase = true;
Hz2Py.m_bReplaceHzNumber = true;
cout << Hz2Py.Hz2Py("深发展(A)", Buf) << endl;
return 0;
}
#define isgb2312head(c) (0xa1<=(uchar)(c) && (uchar)(c)<=0xf7)
#define isgb2312tail(c) (0xa1<=(uchar)(c) && (uchar)(c)<=0xfe)
JavaScript正则表达式
if(/\W/.test(你的子符串)) alert('输入含有汉字或多字节字符');
Option 2
Java正则表达式
java.util.regex.Pattern p = Pattern.compile("\W");
java.util.regex.Matcher m = p.matcher(你的子符串);
boolean f = m.matches();
f == true 含有汉字
String wux=new String(a);
byte[] b=wux.getBytes();
if(b.length>1){
System.out.println("是汉字");
}