C/C++: string类函数使用

str.erase()函数

erase函数原型(三种用法):
(1)string& erase ( size_t pos = 0, size_t n = npos );
(2)iterator erase ( iterator position );
(3)iterator erase ( iterator first, iterator last );

删除指定位置的字符、字符串:
erase(pos,n);
pos参数代表要删除字符的起始位置,默认为0;
n代表要删除的字符串长度,默认为string::npos;返回删除后的字符串
比如erase(0,1)就是删除第一个字符

erase(position);
删除position处的一个字符(position是个string类型的迭代器)

str.erase(str.begin()+9);

erase(first,last);
删除从first到last之间的字符(first和last都是迭代器)

str.erase(str.bengin()+5,str.end()-8);

str.size()、str.length()函数

size()和 length()成员函数均可返回字符串中的字符数;
string::npos 为字符串中可储存的最大字符数。

str.find()函数

函数原型:
(1) size_type find( const string& str, size_type pos=0)
从字符串的pos位置开始,查找子串 str,若找到,则返回首次出现位置的索引,若找不到,返回 string::npos
(2)size_type find( const char* s, size_type pos=0)
从字符串的pos位置开始,查找子串 s,若找到,则返回首次出现位置的索引,若找不到,返回 string::npos
(3) size_type find( const char* s, size_type pos=0,size_type n)
从字符串的pos位置开始,查找 s的前n个字符组成的子字符串,若找到,则返回子字符串首次出现时首字符的位置的索引,若找不到,返回 string::npos
(4) size_type find(char ch , size_type pos=0)
从字符串pos位置开始,查找字符 ch ,若找到,则返回首次出现位置的索引,若找不到,返回 string::npos

除此之外,还定义了
rfind():查找字符串或字符最后一次出现的位置
find_first_of():在字符串中查找参数中任何一个字符首次出现的位置
find_last_of()
find_first_not_of()
find_last_not_of()

str.replace()函数

功能:替换字符串中的元素或者子串,并返回替换后的值。
函数原型:
(1)string& replace (size_t pos, size_t len, const string& str); 用str来替换原字符串从起始位置pos开始,长度为len的字符。
使用例子:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str = "he is@ a@ good boy";
	str=str.replace(str.find("a"),2,"#");  //从第一个a位置开始的两个字符替换成#
	cout<<str<<endl; 
	return 0;
}

(2)string& replace (const_iterator i1, const_iterator i2, const string& str); 用str替换,迭代器起始位置i1到结束位置i2范围的字符。
使用例子:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str = "he is@ a@ good boy";
	 str=str.replace(str.begin(),str.begin()+5,"#"); //用#替换从begin位置开始的5个字符
	 cout<<str<<endl;
	 return 0; 
}

(3)string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen); 用子串substr的给定起始位置与长度,替换原字符串中指定位置上的字符串
使用例子:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str = "he is@ a@ good boy";
	string substr = "12345";
	str=str.replace(0,5,substr,substr.find("1"),4); //用substr的指定字符串替换str指定字符串
	cout << str << endl;
	return 0; 
}

str.append()函数

append用法:用于字符串或字符的拼接
函数原型:
(1) basic_string &append( const char *str ); 在字符串的末尾添加str
(2)basic_string &append( const basic_string &str, size_type index, size_type len ); 在字符串的末尾添加str的子串,子串以index索引开始,长度为len
(3)basic_string &append( const char *str, size_type num ); 在字符串的末尾添加str中的num个字符
(4)basic_string &append( size_type num, char ch );在字符串的末尾添加num个字符ch,
(5) basic_string &append( input_iterator start, input_iterator end ); 在字符串的末尾添加以迭代器start和end表示的字符序列

常用用法:

string str1="I like C++";  
    string str2=",I like the world.";  
    string str3="Hello";  
    string str4("Hi");  
    //====================================  
    str1.append(str2);  
    str3.append(str2, 11, 7);  
    str4.append(5, '.');  
    //====================================  
    cout<<str1<<endl;  
    cout<<str3<<endl;  
    cout<<str4<<endl;  
    system("pause");  
    return 0;     
}  

运行结果:

I like C++,I like the world.
Hello World.
Hi.....

str.substr()函数

substr作用:复制子字符串,从指定位置开始,设定指定长度。
函数原型:string substr (size_t pos = 0, size_t len = npos) const;
使用例子:

/ string::substr
#include <iostream>
#include <string>
 
int main ()
{
  std::string str="We think in generalities, but we live in details.";
                                           // (quoting Alfred N. Whitehead)
  std::string str2 = str.substr (3,5);     // "think"
  std::size_t pos = str.find("live");      // position of "live" in str
  std::string str3 = str.substr (pos);     // get from "live" to the end
  std::cout << str2 << ' ' << str3 << '\n';
 
  return 0;
}

str.assign()函数

assign作用:对字符串进行赋值
(1) string& assign (const string& str); 将字符串str赋值给对象
用法:

string s1;
string s2;
s1.assign(s2);// 与 s1 = s2 等效

(2)string& assign (const string& str, size_t subpos, size_t sublen);将字符串str中的子串赋值给对象,子串为起始位置为subpos,长度为sublen的字符串
用法:

 string str1("hello");
 string str2.assign(str1, 2, 3);
 结果为:
 llo

(3)string& assign (size_t n, char c); 用几个相同的字符赋值
用法:

string str5.assign(10, 'c');
得到:
cccccccccc

(4) string& assign (const char* s, size_t n); 用一个字符串的前n个字符的子串赋值
用法:

string str4.assign("World", 4);
得到:
Worl

str.insert()函数

insert作用:插入字符串或字符
函数原型
(1)basic_string& insert (size_type pos, const basic_string& str); 在原字符串下标为pos的字符前插入字符串str
(2)basic_string& insert (size_type pos, const basic_string& str, size_type pos1, size_type n); 在原字符串下标为pos的字符前,插入str中下标pos1起始的n个字符
(3) basic_string& insert (size_type pos, size_type n, char c);在原字符串下标为pos的字符前插入n个字符c

使用例子:

#include<iostream>
using namespace std;
int main()
{
 string str="hello";
 string s="Hahah";
 str.insert(1,s);//在原串下标为1的字符e前插入字符串s
 cout<<str<<endl;
 
 string str1="hello";
 char c='w';
 str1.insert(4,5,c);//在原串下标为4的字符o前插入5个字符c
 cout<<str1<<endl;
 
 string str2="hello";
 string s2="weakhaha";
 str2.insert(0,s2,1,3);//将字符串s2从下标为1的e开始数3个字符,分别是eak,插入原串的下标为0的字符h前
 cout<<str2<<endl;
 
 return 0;
}

运行结果:
hHahahello
hellwwwwwo
eakhello
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值