替换字符串中的字符
Replacing chars from string
我的代码如下(简化):
CComVariant* input是一个输入参数
CString cstrPath(input ->bstrVal);
const CHAR cInvalidChars[] = {"/*&#^°"§$[]?´`';| "};
for (unsigned int i = 0; i < strlen(cInvalidChars); i++)
{
cstrPath.Replace(cInvalidChars[i],_T(''));
}
调试时,cstrPath的值为L '§',cInvalidChars[7]的值为-89 '§'
我以前尝试过使用。remove(),但问题仍然是一样的:当涉及到§或´时,代码表似乎不匹配,字符不能正确识别,不会被删除。对invalidChars使用TCHAR数组会导致不同的问题('§' -> 'ㅄ')。问题似乎是我没有使用正确的代码表,但到目前为止我所尝试的一切都没有取得任何成功。我想成功地替换/删除任何出现的'§'.
我也看了几篇"从字符串中删除字符"的帖子,但我没有找到任何帮助我的东西。
可执行代码:CComVariant* pccovaValue = new CComVariant();
pccovaValue->bstrVal = L"§§";
const CHAR cInvalidChars[] = {"§"};
CString cstrPath(pccovaValue->bstrVal);
for (unsigned int i = 0; i < strlen(cInvalidChars); i++)
{
cstrPath.Remove(cInvalidChars[i]);
}
cstrPath = cstrPath;
break into cstrPath = cstrPath;
根据评论,你混淆了Unicode和ANSI编码。看起来你的应用程序是针对Unicode的,这很好。您应该完全停止使用ANSI。
这样声明cInvalidChars
:
CString cInvalidChars = L"/*&#^°"§$[]?´`';|";
使用L
前缀意味着字符串字面值是一个宽字符UTF-16字面值。
那么你的循环可以像这样:
for (int i = 0; i < cInvalidChars.GetLength(); i++)
cstrPath.Remove(cInvalidChars[i]);
相关文章:
- 从 argv[1] 转换为字符 * 字符串后有什么问题?
- 将 NULL 与 C 的字符* 字符串一起使用
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- 无法将常量字符字符串传递给模板类
- 如何组合一个宽字符字符串,中间插入一些空字符
- 如何将二维数组类型字符(字符串)作为函数参数传递?
- 从给定索引返回子字符字符串的函数
- 字符 [](c 字符串)的初始化标准
- 如何将字符字符串用作数学运算符
- 将 Unicode 字符/字符串写入文件
- C++,字符* 字符串修改
- 如何有效地用不同的整数元素替换字符字符串的元素
- C++:如果我们在字符串中添加一些整数,为什么它会从开头删除该数量的字符?(字符串 + 整数)
- strcpy正在复制sth字符i字符串.如何解决此错误
- 在C++中将双精度转换为字符*/字符串
- C 中的宽字符字符串
- 如何提取C 中的字符/字符串之间的字符串
- 无法将字符/字符串转换为int
- 带有指针的反转字符字符串
- 反转字符串中的 n 个字符(字符串中没有空格),而不使用 c++ 中的内置函数