如何在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