使用语言环境查找字符串中的子字符串 [英] Find substring in string using locale
问题描述
我需要查找字符串是否包含子字符串,但要根据当前语言环境的规则.
I need to find if a string contains a substring, but according to the current locale's rules.
因此,如果我要搜索字符串"aba",则使用西班牙语语言环境"cabalgar",rábano"和gabán"将全部包含它.
So, if I'm searching for the string "aba", with the Spanish locale, "cabalgar", "rábano" and "gabán" would all three contain it.
我知道我可以将字符串与语言环境信息(整理)进行比较,但是是否有任何内置或starforforward方法可以对find执行相同操作,还是我必须编写自己的方法?
I know I can compare strings with locale information (collate), but is there any built-in or starightforward way to do the same with find, or do I have to write my own?
我可以很好地使用std :: string(最高TR1)或MFC的CString
I'm fine using std::string (up to TR1) or MFC's CString
推荐答案
作为参考,以下是使用通过ICU后端编译的boost语言环境的实现:
For reference, here is an implementation using boost locale compiled with ICU backend:
#include <iostream>
#include <boost/locale.hpp>
namespace bl = boost::locale;
std::locale usedLocale;
std::string normalize(const std::string& input)
{
const bl::collator<char>& collator = std::use_facet<bl::collator<char> >(usedLocale);
return collator.transform(bl::collator_base::primary, input);
}
bool contain(const std::string& op1, const std::string& op2){
std::string normOp2 = normalize(op2);
//Gotcha!! collator.transform() is returning an accessible null byte (\0) at
//the end of the string. Thats why we search till 'normOp2.length()-1'
return normalize(op1).find( normOp2.c_str(), 0, normOp2.length()-1 ) != std::string::npos;
}
int main()
{
bl::generator generator;
usedLocale = generator(""); //use default system locale
std::cout << std::boolalpha
<< contain("cabalgar", "aba") << "\n"
<< contain("rábano", "aba") << "\n"
<< contain("gabán", "aba") << "\n"
<< contain("gabán", "Âbã") << "\n"
<< contain("gabán", "aba.") << "\n"
}
输出:
true
true
true
true
false
这篇关于使用语言环境查找字符串中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!