프로그램

C++ string lower case / upper case 대소문자 변환

(주)CKBcorp., 2022. 4. 27. 08:41
반응형



https://stackoverflow.com/questions/313970/how-to-convert-an-instance-of-stdstring-to-lower-case

결론부터 이야기하면, string.toUpper() / string.toLower() 가 없다. 직접 한글자 한글자 바꾸는 수 밖에.
또, ASCII 일때와 unicode 일 때 대응 방법이 다르다. 



ASCII 기준.  

#include <iostream>
#include <string>
#include <algorithm>
#include <ctype.h>

using namespace std;

int main()
{
string str1;
string str2;

// 1. 
str1 = "AbcD";
for( auto &c : str1 ) 
{
c = tolower( c );
}
cout << str1 << endl;


// 2. 
str1 = "AbcD";
transform( str1.begin(), str1.end(), str1.begin(), ::tolower );
cout << str1 << endl;
}

유니코드 기준.

#include <unicode/unistr.h>
#include <unicode/ustream.h>
#include <unicode/locid.h>

#include <iostream>

int main()
{
    /* "Odysseus" */
    char const * someString = u8"ΟΔΥΣΣΕΥΣ";
    icu::UnicodeString someUString( someString, "UTF-8" );
    // Setting the locale explicitly here for completeness.
    // Usually you would use the user-specified system locale,
    // which *does* make a difference (see ı vs. i above).
    std::cout << someUString.toLower( "el_GR" ) << "\n";
    std::cout << someUString.toUpper( "el_GR" ) << "\n";
    return 0;
}


Compile : 
g++ -Wall example.cpp -licuuc -licuio

결과 : 
ὀδυσσεύς

반응형