如何在C或C++中将多语言字符串或unicode字符串转换为大写/小写字符串。将C++中的unicode字符串转换为大写
回答
对于C在当前线程调整的C语言环境后,我会用toupper
一个相当详细的文件。
setlocale(LC_CTYPE, "en_US.UTF8");
对于C++我会使用的std::ctype<char>
的toupper
方法:
std::locale loc;
auto& f = std::use_facet<std::ctype<char>>(loc);
char str[80] = "Hello World";
f.toupper(str, str+strlen(str));
我的代码是: - 的setlocale(LC_CTYPE, “en_US.UTF8”); std :: locale loc; 自动&F =的std :: use_facet <性病:: CTYPE
@Pankaj那是因为en_US环境没有任何东西,是不是在A-Z的大写形式。 –
@ R.MartinhoFernandes所以我必须这样做,实际上我对CPP非常陌生,我甚至不知道CPP中的hello world program。 一些如何管理.. – Pankaj
设置locale first,例如:
setlocale(LC_ALL, "German")); /*This won't work as per comments below */
setlocale(LC_ALL, "English"));
setlocale(LC_MONETARY, "French");
setlocale(LC_ALL, ""); //default locale
然后使用
std::use_facetstd::locale如下: -
typedef std::string::value_type char_t;
char_t upcase(char_t ch)
{
return std::use_facet< std::ctype<char_t> >(std::locale()).toupper(ch);
}
std::string toupper(const std::string &src)
{
std::string result;
std::transform(src.begin(), src.end(), std::back_inserter(result), upcase);
return result;
}
const std::string src = "Hello World!";
std::cout << toupper(src);
如果您的系统已经是UTF-8,使用std::use_facet
,你可以写:
#include <iostream>
#include <locale.h>
int main() {
std::locale::global(std::locale("")); // (*)
std::wcout.imbue(std::locale());
auto& f = std::use_facet<std::ctype<wchar_t>>(std::locale());
std::wstring str = L"Zoë Saldaña played in La maldición del padre Cardona.";
f.toupper(&str[0], &str[0] + str.size());
std::wcout << str << std::endl;
return 0;
}
,你会得到(http://ideone.com/AFHoHC):
佐伊·索尔达娜扮演LA MALDICIÓNDEL PADRE CARDONA。
如果不工作,你将不得不改变(*)到std::locale::global(std::locale("en_US.UTF8"));
或者你确实对。平台的UTF-8语言环境。
我发现problem_
1. 的setlocale的2溶液(LC_CTYPE “的en_US.UTF-8”); //语言环境将是UTF-8已启用英语
std::wstring str = L"Zoë Saldaña played in La maldición del padre Cardona.ëèñ";
std::wcout << str << std::endl;
for (wstring::iterator it = str.begin(); it != str.end(); ++it)
*it = towupper(*it);
std::wcout << "toUpper_onGCC_LLVM_1 :: "<< str << std::endl;
这是关于LLVM GCC 4.2编译器的工作。
2. std :: locale :: global(std :: locale(“en_US.UTF-8”)); //语言环境将启用UTF-8英语
std::wcout.imbue(std::locale());
const std::ctype<wchar_t>& f = std::use_facet< std::ctype<wchar_t> >(std::locale());
std::wstring str = L"Chloëè";//"Zoë Saldaña played in La maldición del padre Cardona.";
f.toupper(&str[0], &str[0] + str.size());
std::wcout << str << std::endl;
这是Apple LLVM 4.2中的工作。
这两种情况下,我跑在Xocde上。 但我找到一种方法来在Eclipse中使用g ++编译器运行此代码。
你读过其他答案吗?你基本上复制/粘贴@ mewa's和我的答案...... –
在Windows中,对于语言环境未知的混合语言应用程序,请考虑CharUpperBuffW
和CharLowerBuffW
。这些函数处理toupper()
没有的变音符号。
如果你想有一个理智和成熟的解决方案,看看IBM's ICU。这里有一个例子:
#include <iostream>
#include <unicode/unistr.h>
#include <string>
int main(){
icu::UnicodeString us("óóßChloë");
us.toUpper(); //convert to uppercase in-place
std::string s;
us.toUTF8String(s);
std::cout<<"Upper: "<<s<<"\n";
us.toLower(); //convert to lowercase in-place
s.clear();
us.toUTF8String(s);
std::cout<<"Lower: "<<s<<"\n";
return 0;
}
输出:
Upper: ÓÓSSCHLOË
Lower: óósschloë
注:在后面的步骤SS
不被视为德国ß
的资本
这看起来像一个不错的图书馆。但是,它的时钟速度接近30MB。我的整个应用程序是21MB。 – Pierre
- 1. C#将Unicode转换为字符串
- 2. 如何将Unicode字符转换为大写的C++
- 3. 将字符串转换为C#Unicode字符文字的序列
- 4. 在Unicode中将unicode字符串转换为可用的unicode
- 5. 如何将字符串转换为Perl中的unicode字符串
- 6. 如何将包含unicode字符的字符串转换为unicode?
- 7. mySQL将字符串中的字符转换为大写
- 8. 将unicode字符串转换为float
- 9. NSBatchUpdateRequest将字符串转换为unicode
- 10. 将unicode字符串转换为utf8
- 11. 将AnsiString转换为Unicode字符串
- 12. 将Unicode转换为字符串Java
- 13. 将字符串转换为unicode
- 14. 将unicode转换为字符串
- 15. 何时将unicode转换为字符串?
- 16. 将Unicode字符串转换为ASCII
- 17. 如何将UNICODE字符串转换为c#中的MBCS?
- 18. 将字符串转换为C中的Unicode
- 19. 如何将unicode字符串转换为C#中的int?
- 20. 将unicode转换为实际字符C#
- 21. 将字符串中特定字符转换为大写
- 22. 如何将Unicode转义序列转换为.NET字符串中的Unicode字符?
- 23. 将Unicode转换为字符
- 24. Unicode转换为Java中的字符串
- 25. 将url编码的字符串转换为python unicode字符串
- 26. 如何将Unicode编码的字符串转换为字符串
- 27. 将unicode字符串转换为Ruby中的字符?
- 28. 将字符串中的多个Unicode转换为字符
- 29. 将RTF字符串中的unicode字符转换为纯文本
- 30. 将unicode codepoint转换为Ruby中的字符串字符
检查此线程的http://计算器的.com /问题/ 1614595 /转换-宽炭字符串到小写式-C。它可能会帮助你。 – AnkitJain
“Unicode”是什么意思? Unicode标准定义了许多不同的编码 - 您使用哪一种编码? UTF-8? UTF-16? UTF-32? –
这里 – Backtrack