代碼如下:
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 '\0'
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.uj5u.com/houduan/288024.html
標籤:C++
