代码如下:

void SomeClass::SomeFunctionToCorrectName(CString &strName)
{
        
    //  Only alphabets (Aa-Zz), numbers(0-9), "_" (underscore) and "-" (hyphen) are allowed. 
                
        for(int nIndex = 0; nIndex < strName.GetLength(); nIndex++)
        {
            TCHAR currentChar = strName.GetAt(nIndex);
            
            if((currentChar >= _T('a') && currentChar <= _T('z')) ||
                (currentChar >= _T('A') && currentChar <= _T('Z')) ||
                (currentChar >= _T('0') && currentChar <= _T('9')) ||
                currentChar == _T('-') || currentChar == _T('_'))
            {
                continue;
            }
            strName.Replace(currentChar,_T(''));    
        }
}

此方法删除strName中的任何额外内容,并且只允许使用(Aa-Zz)、numbers(0-9)、{(下划线)和“-”(连字符)。国际单项体育联合会要检查那些允许的条件。如果它不在允许的条件下,它将删除它。

For Eg desired i/p :"Hel*lo*World" and desired o/p : "HelloWorld"

但下面给出的错误如下:

error C2137: empty character constant

I can fix this error by using any of the three methods:
1. using ''
2. using ' ' (space in between ' ')
3. using ""[0]

但这会在更换时引入空间。

Input :Hel*lo*World
Output :Hel lo World
Desired Output :HelloWorld

有人能告诉我怎样才能得到想要的结果吗?

如果要删除特定字符,则Replace不是正确的方法。正如您所注意到的,没有任何合理的方法可以代替这个角色。

相反,您可以像这样使用Remove

strName.Remove(currentChar);

refer to:https://www.5axxw.com/questions/content/ksso5b
内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/tewuapple/p/14892161.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程

4775 0元 限免