您的位置:首页 > 编程语言 > C语言/C++

C++中的String用法

2008-10-17 11:56 363 查看
本文转自 http://blog.programfan.com/article.asp?id=16880 && http://blog.programfan.com/article.asp?id=16881 && http://blog.programfan.com/article.asp?id=16882


basic_string::append
向string 的后面加字符或字符串。(比+=, push_back 更灵活)
(1)向string 的后面加C-string
basic_string& append( const value_type* _Ptr );
string s ( "Hello " ); // s=”Hello ”
const char *c = "Out There ";
s.append ( c ); // s=”Hello Out There”
(2)向string 的后面加C-string 的一部分
basic_string& append( const value_type* _Ptr, size_type _Count );
string s ( "Hello " ); // s=”Hello ”
const char *c = "Out There ";
s.append ( c , 3 ); // s=”Hello Out”
(3)向string 的后面加string(有两种方法)
basic_string& append( const basic_string& _Str );
string s1 ( "Hello " ), s2 ( "Wide " ), s3( "World " );
s1.append ( s2 ); // s1=”Hello Wide”
s1 += s3; // s1=”Hello Wide World”
(4)向string 的后面加string 的一部分 ---A
basic_string& append( const basic_string& _Str, size_type _Off,
size_type _Count );
string s1 ( "Hello " ), s2 ( "Wide World " );
s1.append ( s2 , 5 , 5 ); // s1=”Hello World”
(5)向string 的后面加string 的一部分 ---B
template<class InputIterator> basic_string& append(
InputIterator _First, InputIterator _Last );
string str1f ( "Hello " ), str2f ( "Wide World" );
str1f.append ( str2f.begin ( ) + 5 , str2f.end ( ) );
// s1=”Hello World”
(6)向string 的后面加多个字符
basic_string& append( size_type _Count, value_type _Ch );
string str1e ( "Hello " );
str1e.append ( 4 , '!' ); // s1=”Hello !!!!”

basic_string::assign
给string 赋值。 (比“=”更灵活)
(1)向string 赋C-string
basic_string& assign( const value_type* _Ptr );
string s;
const char *c = "Out There";
s.assign ( c ); // s=”Out There”
(2)向string 赋C-string 的一部分
basic_string& assign( const value_type* _Ptr, size_type _Count );
string s;
const char *c = "Out There";
s.assign ( c , 3 ); // s=”Out”
(3)向string 赋string(有两种方法)
basic_string& assign( const basic_string& _Str );
string s1 ( "Hello" ), s2 ( "Wide" ), s3( "World" );
s1.assign ( s2 ); // s1=”Wide”
s1 = s3; // s1=”World”
(4)向string 赋string 的一部分 ---A
basic_string& assign( const basic_string& _Str, size_type off,
size_type _Count );
string s1 ( "Hello " ), s2 ( "Wide World " );
s1.assign ( s2 , 5 , 5 ); // s1=”Hello World”
(5)向string 赋string 的一部分 ---B
template<class InIt> basic_string& assign(
InputIterator _First,
InputIterator _Last );
string str1f ( "Hello " ), str2f ( "Wide World" );
str1f.assign ( str2f.begin ( ) + 5 , str2f.end ( ) ); // s1=”Wide World”
(6)向string 赋 多个字符
basic_string& assign( size_type _Count, value_type _Ch );
string str1e ( "Hello " );
str1e.assign ( 4 , '!' ); // s1=”!!!!”

basic_string::compare
如果所比较的两个string 相等,则返回0; 操作string 大于参数string,返回
正数;操作string 小于参数string,返回负数。
(1)比较操作string 与_Str 或C-string_Ptr
int compare( const basic_string& _Str ) const;
int compare( const value_type* _Ptr ) const;
int com = s.compare ( sp );
(2)比较操作string 中_Pos1(下标)开始的_Num1 个字符
与 string_Str
比较操作string 中_Pos1(下标)开始的_Num1 个字符 与 C-string _Ptr
比较操作string 中Pos1(下标)开始的Num1 个字符 与Str 中Off(下标)开始Count 个字

int compare( size_type _Pos1, size_type _Num1, const basic_string& _Str );
int compare( size_type _Pos1, size_type _Num1, const value_type* _Ptr ) const;
int compare( size_type _Pos1, size_type _Num1, const basic_string& _Str,
size_type _Off, size_type _Count );
int com1 = s.compare ( 2 , 3 , sp );
int com2 = s.compare ( 2 , 3 , c );
int com3 = s.compare ( 1 , 3 , cs , 3 ,1 );

basic_string::erase
删除string 中的一个或几个元素。前两个成员函数,返回要被删除的子串的下
一个元素的iterator; 第三个函数,返回删除后的string 的引用。
(1)删除string 中从_First 到_Last 的字符
iterator erase( iterator _First, iterator _Last );
basic_string <char>::iterator s_Iter;
s_Iter = s.erase ( s.begin ( ) + 3 , s.end ( ) - 1 ); // s_Iter=s.end( )
(2) 删除string 中_It 所指的字符
iterator erase( iterator _It );
s_Iter = s.erase ( s.begin ( ) + 5 );
(3) 删除string 中从_Pos(下标)开始的_Count 个字符
basic_string& erase( size_type _Pos = 0, size_type _Count = npos );
str = s.erase ( 6 , 8 ); // str 也是string

basic_string::find
寻找给定的string。返回找到的第一个string 下标值;如果没找到则返回npos。
(1)找一个character_Ch。(默认从头找)
size_type find( value_type _Ch, size_type _Off = 0 ) const;
string s ( "Hello Everyone" );
basic_string <char>::size_type index1, index2;
static const basic_string <char>::size_type npos = -1;
index1 = s.find ( "e" , 3 ); // index1=8,不是6
index2 = s.find ( "x" ); // index2=-1
if (indexCh1a != npos ) cout <<indexCh1a << endl;
else cout << "The character 'e' was not found in str1 ." << endl;
(2)找一个C-string。(默认从头找)
size_type find( const value_type* _Ptr, size_type _Off = 0 ) const;
string s ( "Let me make this perfectly clear." );
basic_string <char>::size_type index;
const char *c = "perfect";
index = s.find ( c , 5 ); // index=17
(3)找一个string。(默认从头找)
size_type find( const basic_string& _Str, size_type _Off = 0 ) const;
string s ( "clearly this perfectly unclear." );
basic_string <char>::size_type index;
string sta ( "clear" );
index = s.find ( sta , 5 ); // index=24

basic_string::max_size
返回string 能放的最大元素个数。(不同于capacity)
size_type max_size( ) const;
basic_string <char>::size_type cap, max;
cap = s.capacity ( );
max = s.max_size ( ); // max=4294967294.

basic_string::rfind
寻找给定的string。返回找到的第一个string 下标值;如果没找到则返回npos。
与find 不同的是:rfind 默认从npos 开始找。其他相同。

basic_string::replace
将原string 中的元素或子串替换。返回替换后的string。
(1)用string 或C-string 代替操作string 中从_Pos1 开始的_Num1 个字符
basic_string& replace( size_type _Pos1,size_type _Num1, const value_type* _Ptr);
basic_string& replace(size_type _Pos1,size_type _Num1,const basic_string_Str);
string a,b;
string s ( "AAAAAAAA" );
string s1p ( "BBB" );
const char* cs1p = "CCC";
a = s.replace ( 1 , 3 , s1p ); // s=”ABBBAAAA”
b = s.replace ( 5 , 3 , cs1p ); // s=”ABBBACCC”
(2)用string 中从_Pos2 开始的_Num2 个字符,代替操作string 中从_Pos1 开始的_Num1 个字符
用C-string 中的_Num2 个字符,代替操作string 中从_Pos1 开始的_Num1 个字符
basic_string& replace( size_type _Pos1, size_type _Num1, const basic_string& _Str,
size_type _Pos2, size_type );
basic_string& replace( size_type _Pos1, size_type _Num1,
const value_type* _Ptr, size_type _Num2 );
string a, b;
string s ( "AAAAAAAA" );
string s2p ( "BBB" );
const char* cs2p = "CCC";
a = s.replace ( 1 , 3 , s2p , 1 , 2 ); // s=”ABBAAAA”
b = s.replace ( 4 , 3 , cs2p , 1 ); // s=”ABBAC”
(3)用_Count 个character_Ch ,代替操作string 中从_Pos1 开始的_Num1 个字符
basic_string& replace( size_type _Pos1, size_type _Num1,
size_type_Count, value_type_Ch );
string result;
string s ( "AAAAAAAA" );
char ch = 'C';
result = s.replace ( 1 , 3 , 4 , ch ); // s=”ACCCCAAAA”
(4)用string 或C-string ,代替操作string 中从First0 到Last0 的字符
basic_string&replace(iterator First0,iterator Last0, const basic_string& _Str);
basic_string&replace(iterator First0,iterator _Last0, const value_type* _Ptr);
string s ( "AAAAAAAA" ); string s4p ( "BBB" );
const char* cs4p = "CCC";
basic_string<char>::iterator IterF0, IterL0;
IterF0 = s.begin ( ); IterL0 = s.begin ( ) + 3;
string a, b;
a = s.replace ( IterF0 , IterL0 , s4p ); // s=”BBBAAAAA”
b = s.replace ( IterF0 , IterL0 , cs4p ); // s=”CCCAAAAA”
(5)用string 中从_Pos2 开始的_Num2 个字符,代替操作string 中从First0 到Last0 的字符
用C-string 中的_Num2 个字符,代替操作string 中从First0 到Last0 的字符
basic_string& replace( iterator _First0, iterator _Last0,
const value_type* _Ptr, size_type _Num2 );
template<class InputIterator> basic_string& replace(
iterator _First0, iterator _Last0,
InputIterator _First, InputIterator _Last );
IterF3 = s.begin ( ) + 1; IterL3 = s.begin ( ) + 3;
IterF4 = s.begin ( ); IterL4 = s.begin ( ) + 2;
a = s.replace ( IterF3 , IterL3 , IterF4 , IterL4 );
b = s.replace ( IterF1 , IterL1 , cs5p , 4 );
(6)用_Count 个character_Ch ,代替操作string 中从First0 到Last0 的字符
basic_string& replace( iterator _First0, iterator _Last0,
size_type _Count , value_type _Ch );
a = s.replace ( IterF2 , IterL2 , 4 , ch );

basic_string::swap
交换两个string。
void swap( basic_string& _Str );
s1.swap ( s2 );

basic_string::substr
返回从_Off(下标)开始的_Count 个字符组成的string
basic_string substr( size_type _Off = 0, size_type _Count = npos ) const;
string s("I love you!"), sub;
sub=s.substr( ); // sub=”I love you!”
sub=s.substr(1); // sub=” love you!”
sub=s.substr(3,4); // sub=”ove ”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: