string
string name_1;
string name_2 ( "LiueDeHua" ) ;
string name_3 ( name_2) ;
string name_4 ( 5 , 'l' ) ;
string name = "LiuDeHua" ;
string s;
cin >> s;
while ( cin >> s) {
cout << s << endl;
}
string strline;
getline ( cin, strline) ;
string str ( "Hello”) ;
string: size_type size = str. size ( ) ;
cout << "str size, char number is " << size << endl;
if ( str. size ( ) == 0 ) ;
if ( str. empty ( ) ) ;
string a ( "HELLO" ) ;
string b ( "WORLD" ) ;
string c;
a + = b;
a = a + "endl" ;
c = "hello " + "world" ;
Subscript operations are used as lvalues
string str ( "This is a line text" ) ;
for ( string:: size_type i = 0 ; i < str. size ( ) ; ++ i) {
str[ i] = '^' ;
}
cout << str << endl;
C & C++ character processing functions
#include <ctype.h>
#include <cctype>
string str ( "This is 1 line text!^_^!" ) ;
string upper_str;
string lower_str;
string:: size_type alnum_cnt = 0 ;
string:: size_type alpha_cnt = 0 ;
string:: size_type cntrl_cnt = 0 ;
string:: size_type digit_cnt = 0 ;
string:: size_type lower_cnt = 0 ;
string:: size_type upper_cnt = 0 ;
string:: size_type punct_cnt = 0 ;
for ( string:: size_type i = 0 ; i < str. size ( ) ; i++ ) {
if ( isalnum ( str[ i] )
++ alnum_cnt;
if ( isalpna ( str[ i] )
++ alpah_cnt;
if ( iscntrl ( str[ i] )
++ cntrl_cnt;
if ( isdigit ( str[ i] )
++ digit_cnt;
if ( islower ( str[ i) )
++ lower_cnt;
if ( isupper ( str[ i] )
++ upper_cnt;
if ( ispunct ( str[ i] )
++ punct_cnt;
upper_str + = toupper ( str[ i) ;
lower_str + = tolower ( str[ i] ) ;
}
string s ( " H e l l W o r l d " ) ;
if ( ! s. empty ( ) ) {
s. erase ( 0 , s. find_first_not_of ( ' ' ) ) ;
s. erase ( s. find_last_not_of ( ' ' ) + 1 ) ;
}
int index = 0 ;
if ( ! s. empty ( ) ) {
while ( ( index = s. find ( ' ' , index) ) != string:: npos ) {
s. erase ( index, 1 ) ;
}
}