如何判断字符串中是否包含英文字母?

annywoody 2008-12-29 02:51:04

CString strTemp = "12A456";
CString strNum = "123456";

如何判断字符串中是否含有英文字母??
...全文
2300 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
路人乙2019 2008-12-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zzz822163 的回复:]
用isalpha 逐个字母检查

C/C++ code
#include <locale>
#include <iostream>

using namespace std;

int main( )
{
locale loc ( "German_Germany" );
bool result1 = isalpha ( 'L', loc);
bool result2 = isalpha ( '@', loc);
bool result3 = isalpha ( '3', loc);

if ( result1 )
cout << "The character 'L' in the locale is "
<< "alphabetic." << endl;
el…
[/Quote]正解。
cofanz 2008-12-30
  • 打赏
  • 举报
回复
不用处理全角的吗?
wmpkumse 2008-12-29
  • 打赏
  • 举报
回复
正则表达式???
一条晚起的虫 2008-12-29
  • 打赏
  • 举报
回复
//只是代码短一点,效率未必高,未进行效率比较测试。
CString strTemp = "123A23";
CString str;
sscanf(strTemp, "%[^A-Za-z]", str);
if(str.GetLength() < strTemp.GetLength())
MessageBox("含有字母");
ccpaishi 2008-12-29
  • 打赏
  • 举报
回复
isalpha是判断是否是字母的最好办法。
用户 昵称 2008-12-29
  • 打赏
  • 举报
回复
一般都从前向后查找,如果要考虑中文的话,那最好用unicode。
cnzdgs 2008-12-29
  • 打赏
  • 举报
回复
大家都没考虑汉字。
dxk01 2008-12-29
  • 打赏
  • 举报
回复
顶一下,有这么多方法了
rookieme 2008-12-29
  • 打赏
  • 举报
回复
bool HaveAtChar(CString c_str)//如果c_str中有字母,返回TRUE,否则返回FALSE
{
int iLength=c_str.GetLength();
c_str.MakeUpper();
char *ptr=c_str.GetBuffer(0);
for(int i=0;i<iLength;i++)
{
if((*ptr>='A')&&(*ptr<='Z'))
return TRUE;
ptr++;
}
return FALSE;

}
annywoody 2008-12-29
  • 打赏
  • 举报
回复
3#的方法想到了,还以为有什么更方便的方法呢
谢谢诸位了
yuhudie203 2008-12-29
  • 打赏
  • 举报
回复
上面for那句有个错误应该是(int i = 0; i <length ; i++)
yuhudie203 2008-12-29
  • 打赏
  • 举报
回复
bool bflag = false;
int length = str.GetLength();
for(int i -= 0 ; i < length ; i++)
{
int num = str.GetAt(i);
if((num>='a'&&num<='z')||(num>='A'&&num<='Z'))
{
bflag = true;
break;
}
}
if(bflag == true)
MessageBox("此字符串含有英文字母");
else
MessageBox("此字符串不含有英文字母");
zzz822163 2008-12-29
  • 打赏
  • 举报
回复
用isalpha 逐个字母检查

#include <locale>
#include <iostream>

using namespace std;

int main( )
{
locale loc ( "German_Germany" );
bool result1 = isalpha ( 'L', loc);
bool result2 = isalpha ( '@', loc);
bool result3 = isalpha ( '3', loc);

if ( result1 )
cout << "The character 'L' in the locale is "
<< "alphabetic." << endl;
else
cout << "The character 'L' in the locale is "
<< " not alphabetic." << endl;

if ( result2 )
cout << "The character '@' in the locale is "
<< "alphabetic." << endl;
else
cout << "The character '@' in the locale is "
<< " not alphabetic." << endl;

if ( result3 )
cout << "The character '3' in the locale is "
<< "alphabetic." << endl;
else
cout << "The character '3' in the locale is "
<< " not alphabetic." << endl;
}
fox000002 2008-12-29
  • 打赏
  • 举报
回复
可以逐个字符判断

isalpha(strTemp[i])

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧