string类型的常用方法

1. 在尾部插入/删除元素

string s("hello");
	
// 插入/删除一个字符 
s.push_back('!');
s.pop_back();
	
// 插入多个字符
s.append("world");
s.insert(s.size(), "world");

// 删除多个字符
s.erase(s.size()-5, 5);
s.erase(s.end()-5, s.end()); 

2. 在字符串中查找

string s("hello");
string s2(s);
char c = 'l';
	
// 查找s中是否有字符c
s.find(c);			// 与下面等价,默认从位置0开始查找 
s.find(c, 0);		// 从s的位置0开始查找字符c
	
// 查找s中是否有字符串s2
s.find(s2); 		// 默认从位置0开始查找 
s.find(s2, 0); 		// 从s的位置0开始查找字符串s2 
	
// 其他查找 
s.find_first_of(s2);	// 返回s2中任意一个字符在s中第一次出现的位置 
s.find_first_not_of(s2);// 返回s中第一个s2没有的字符的位置 

  

  

  

原文地址:https://www.cnblogs.com/xzxl/p/9605843.html