【发布时间】:2010-09-13 18:31:29
【问题描述】:
我对 C++ 中的一个字符串有疑问,该字符串在西班牙语中有几个单词。这意味着我有很多带有重音符号和波浪线的单词。我想将它们替换为没有重音的对应物。示例:我想替换这个词:“había”代表 habia。我尝试直接替换它,但使用字符串类的替换方法,但我无法让它工作。
我正在使用此代码:
for (it= dictionary.begin(); it != dictionary.end(); it++)
{
strMine=(it->first);
found=toReplace.find_first_of(strMine);
while (found!=std::string::npos)
{
strAux=(it->second);
toReplace.erase(found,strMine.length());
toReplace.insert(found,strAux);
found=toReplace.find_first_of(strMine,found+1);
}
}
dictionary 是这样的地图(有更多条目):
dictionary.insert ( std::pair<std::string,std::string>("á","a") );
dictionary.insert ( std::pair<std::string,std::string>("é","e") );
dictionary.insert ( std::pair<std::string,std::string>("í","i") );
dictionary.insert ( std::pair<std::string,std::string>("ó","o") );
dictionary.insert ( std::pair<std::string,std::string>("ú","u") );
dictionary.insert ( std::pair<std::string,std::string>("ñ","n") );
而toReplace 字符串是:
std::string toReplace="á-é-í-ó-ú-ñ-á-é-í-ó-ú-ñ";
我显然一定错过了什么。我想不通。 有没有我可以使用的库?
谢谢,
【问题讨论】:
-
您应该添加您所针对的平台(Windows、Linux 等),以及您所针对的编码(UTF-8、UTF-16 等)。例如,您的“á”是字形 E1,它在 USO-8859-1 char 上翻译 'á',在 UTF-16 wchar_t 上翻译 L'á',但在 UTF-8 上翻译“á”(是的,两个字符)
-
对不起...当我回到您的帖子(通过在 Unicode.org 上搜索)并验证评论时,您确实回答了...
-
这是 stackoverflow.com/questions/140422/…"> 如何将 8 位字符转换为 7 位字符的副本? (即 Ü 到 U).
标签: c++ string text str-replace