我有 tchar*
字符串 The system time has changed to 2018 - 09 - 06T15:13 : 52.257364700Z from 2018 - 09 - 06T15 : 13 : 52.257364700Z.
当我把那个字符串here我看到我的日期值周围的字符时,当我使用 wPrintf
打印它时,我在那些位置得到问号 .
有没有办法迭代 tchar*
并删除非ASCII字符?
int main() {
const TCHAR *pText = _T("The system time has changed to 2018 - 09 - 06T15:13 : 52.257364700Z from 2018 - 09 - 06T15 : 13 : 52.257364700Z.");
TCHAR* temp;
temp = removet((TCHAR*)pText, _tcslen(pText));
wprintf(_T("%s"), temp);
}
TCHAR* removet(TCHAR* text, int len) {
int offset = 0;
for (int i = 0; text[i] != 0; ++i) {
if (text[i] > 127) {
offset++;
}
if (!((i + offset) > len)) {
wprintf(_T("%d"), i +offset);
text[i] = text[i + offset];
}
}
return text;
}
更正代码:
int main() {
const TCHAR *pText = _T("The system time has changed to 2018 - 09 - 06T15:13 : 52.257364700Z from 2018 - 09 - 06T15 : 13 : 52.257364700Z.");
TCHAR* temp;
temp = removet((TCHAR*)pText, _tcslen(pText));
wprintf(_T("%s"), temp);
}
TCHAR* removet(TCHAR* text, int len) {
int offset = 0;
TCHAR* str2 = new TCHAR[len+1];
_tcscpy_s(str2, len+1, text);
for (int i = 0; str2[i] != 0; ++i) {
if (str2[i+offset] > 127) {
offset++;
}
if (!((i + offset) >= len)) {
str2[i] = str2[i + offset];
}
}
return str2;
}
1 回答
如果您使用
std::string
而不是原始字符数组,这将更容易,但您仍然可以使用一些c功能:并使用
std::basic_string
: