(작성일 : 2008.04.03, 티스토리로 이사하며 옮긴 글)

 

erase() 를 어케 써볼까 했더만,
찾아보니 간단한게 있네요. 쪼아~ ^^

 

======================================================
아쉽게도 VC++이나 Java에서 지원하는 trim() 함수가 STL String에는 없습니다. 따라서, 간단하게 아래 코드를 사용하시면 되겠습니다.

 

1. string의 왼편의 white space를 trim하고 싶은 경우
using namespace std;
string str("hello");
int n = str.find_last_not_of("tvn");
if ( n != string::npos )
str.replace(n+1, str.length()-n,"");
cout<<str;

 

2. string의 오른편의 white space를 trim하고 싶은 경우
using namespace std;
string str("hello");
int n = str.find_first_not_of("tvn");
if ( n != string::npos )
str.replace(0, n,"");
cout<<str;

 

3. string의 양쪽의 white space를 trim하고 싶은 경우
using namespace std;
string str("hello");
int n;
n = str.find_first_not_of("tvn");
if ( n != string::npos )
str.replace(0, n,"");
n = str.find_last_not_of("tvn");
if ( n != string::npos )
str.replace(n+1, str.length()-n,"");
cout<<str;

+ Recent posts