C++の検査に特殊文字が含まれているか
//
bool checkTextChineseOrNumberOrLetter(string str)
{
int badNum = 0;
int size = str.length();
if (size <= 0)
return false;
char* pStr = new char[size];
strcpy(pStr, str.c_str());
for (int i = 0; i < size; i++)
{
if (!(pStr[i]>=0 && pStr[i]<=127))
continue;
if (ispunct(pStr[i]))
{
badNum ++;
}
}
delete[] pStr;
bool res = true;
if (badNum > 0)
{
res = false;
}
return res;
}
int main()
{
string szU8 = "asdfghgh 1234567890 ¥%@#%#¥……&%¥&¥$$$ ";
if (checkTextChineseOrNumberOrLetter(szU8))
{
cout << " " << endl;
}
else
{
cout << " " << endl;
}
getchar();
return 0;
}
中国語のコードかどうかを判断します.
if (!(pStr[i]>=0 && pStr[i]<=127))
continue;
ispunct(int)関数はcctypeである.hにおける関数の目的:文字が句読点または特殊記号であるかどうかをテストする.
この関数を簡単に修正することで、特殊文字のフィルタリングも完了します.ここでは余計な負担にすぎない.