Showing posts with label std::string. Show all posts
Showing posts with label std::string. Show all posts

2009/03/07

[STL] std::string 대소문자 바꾸기

std::string 의 문자열을 대소문자 변환이 필요해 찾아보았는데 쉽게 처리할 수 있는 방법을 찾았다.

std::transform 함와 cctype 헤더의 tolower() 와 toupper() 함수를 이용하면 쉽게 해결이 되었다.

transform 함수는 이외에도 응용할 부분이 많아보인다. container 의 각 원소에 특정을 변형을 주어
바꾸거나 다는 container에 넣을 수도 있다. transform 함수에 대해서는 좀더 공부를 해서 나중에 포스팅을 한번 해야겠다.

#include <cctype> // for toupper & tolower
#include <string>
#include <algorithm>
using namespace std;


// 대문자로 바꾸기
string s1 = "sample string";
transform( s1.begin(), s1.end(), s1.begin(), toupper );
// 결과 : s1 "SAMPLE STRING"

// 소문자로 바꾸기
string s2 = "HELLO";
transform( s2.begin(), s2.end(), s2.begin(), tolower );
// 결과 : s2 "hello"


// 첫 문자만 대문자로 바꾸기
string s3 = "title";
transform( s3.begin(), s3.begin() + 1, s3.begin(), toupper );
// 결과 : s3 "Title"

Original Post : http://neodreamer-dev.tistory.com/267

2009/03/05

UTF8 문자열을 std::string 이나 CString 으로 변환하기

다국어 프로그래밍에서 RapidXML 을 이용하다가 필요하여 만들어 본 문자열 변환 함수 이다.



////////////////////////////////////////////////////////////////
// UTF8 문자열은 std::string 으로 변환하기
void CMultiLang::UTF82A( IN const char* utf8, OUT string& ansi )
{
int length = MultiByteToWideChar( CP_UTF8, 0, utf8, (int)strlen(utf8) + 1, NULL, NULL );
wchar_t* pBuf = new wchar_t[ length + 1 ];

MultiByteToWideChar( CP_UTF8, 0, utf8, (int)strlen(utf8) + 1, pBuf, length );

pBuf[length] = 0;
ansi = CStringA( pBuf );

delete [] pBuf;
}

////////////////////////////////////////////////////////////////
// UTF8 문자열은 CString 으로 변환하기
void CMultiLang::UTF82T( IN const char* utf8, OUT CString& out )
{
int length = MultiByteToWideChar( CP_UTF8, 0, utf8, (int)strlen(utf8) + 1, NULL, NULL );
wchar_t* pBuf = new wchar_t[ length + 1 ];

MultiByteToWideChar( CP_UTF8, 0, utf8, (int)strlen(utf8) + 1, pBuf, length );

pBuf[length] = 0;
out = CString( pBuf );

delete [] pBuf;
}

Original Post : http://neodreamer-dev.tistory.com/264

2008/12/25

std::string 의 날짜를 CTime에 넣기

MySQL의 DATE 함수에 의해 얻어진 std::string 문자열("YYY-MM-DD")을 CTime에 넣기.

        // "1999-01-01"
// "0123456789"
std::string d = "1999-01-01";

CTime time(
atoi( d.substr(0, 4).c_str() ), // Year
atoi( d.substr(5, 2).c_str() ), // Month
atoi( d.substr(8, 2).c_str() ), // Day
0, 0, 0 /* Time */ );


Original Post : http://neodreamer-dev.tistory.com/229