加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

c++ string 的函数replace()用法

(2011-05-12 22:46:54)
标签:

[转]

c

string

replace()用法

杂谈

分类: 转载

[转] c++ string 的函数replace()用法

【原帖地址】

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 );

http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifstring 的函数replace()用法" TITLE="c++ string 的函数replace()用法" />Code
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 ); //

(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 );

http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifstring 的函数replace()用法" TITLE="c++ string 的函数replace()用法" />Code
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 ); //

(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 );

http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifstring 的函数replace()用法" TITLE="c++ string 的函数replace()用法" />Code
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 ); //

(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"

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有