转载自百度经验 :https://jingyan.baidu.com/article/20b68a8854f919796dec6265.html?qq-pf-to=pcqq.c2c
string类的常用方法有哪些。string查找替换、分割字符串、比较、截取、类型转换、排序等功能都提供了强大的处理函数,可以代替字符数组来使用。
1. 定义和构造初始化
string 提供了很多构造函数,可以以多种方式来初始化string字符串
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
string str1="yesterday once more";
string str2 ("my heart go on"); // = day once more
string str3(str1,6); //= day
string str4 (str1,6,3);
char ch_music[]= {"Roly-Poly"};
string str5 = ch_music;
string str6 (ch_music);
string str7 (ch_music,4); //= Roly
string str8(10,'i'); // = iiiiiiiii
string str9 (ch_music + 5,ch_music + 9);//= Poly
str9.~string(); // 销毁字符,释放内存
getchar();
return 0;
}
2.赋值,拼接字符串
string重载了 = + += 等多种运算符,让字符串组合拼接更简单。
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
string str = "A woman appreciates ";
string str2 = " a man who can make her laugh";
string strlove = str + str2;
cout << strlove <<endl;
str += str2;
cout << str <<endl;
str.push_back('.');
str.append("阿拉丁"); //通过在其当前值的末尾附加附加字符来扩展字符串:
str.assign("dreams come true "); //重新赋值
str.insert(16,"!!!!"); //在指定位置插入字符
cout << str <<endl;
return 0;
}
3.访问字符操作
string可以按数组方式,以下标来访问。还可以用at()函数访问指定的字符。
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
string strip4 = "iPhone4";
cout << strip4[2]; // = h
cout << strip4.at(5); // = e
string stuff;
getline(cin, stuff); //输入一行字符,赋值给stuff
//输入一样字符,赋值给stuff, 以! 结束
getline(cin, stuff, '!');
cout << stuff;
getchar();
return 0;
}
4.可以使用 STL 的接口
可以把 string 理解为一个特殊的容器,容器中装的是字符
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
string str;
str.push_back('Q');
str.push_back('A');
string::iterator itstr = str.begin();
for (; itstr != str.end(); itstr++ )
{
cout << *itstr;
}
sort (str.begin(), str.end()); //排序 要头文件algorithm
str.pop_back();
getchar();
return 0;
}
5.比较操作 == != > >= < <= compare 等
string的比较操作,按字符在字典中的顺序进行逐一比较。在字典前面的字符小于后面的字符。
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
string str = "hehe";
if (str < "hello")
{
cout << "hehe 小于 hello" << endl;
}
string str2 = "anhui";
string str3 = "hunan";
string str4 = "nanjing";
if (str2 >=str3 || str2 ==str4)
{
cout << "安徽-南京-湖南";
}
str2.compare("anhui"); //相等
str2.compare("str3"); //相等返回0 大于返回正数, 小于返回负数
str3.compare(3,2,str4,2,2); //na比较nj
cout << str4.compare(3,4,"jing",4); //jing == jing 输出0
getchar();
return 0;
}
6.查找 find rfind
string中除了find、rfind,还有find_first_of等函数也提供了强大的查找功能。
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
string str = "when i was young, i listen to the radio. ";
string::size_type position;
//find 函数返回listen 在str中第一次出现的下标位置
position = str.find("listen");
if (position != str.npos) //如果没找到,返回npos, 这个值很大4294967295
{
cout << "第一次出现的下标是:" << position <<endl;
}
//从字符串str 下标 9 开始,查找字符串 you, 返回 you 在 str 中的下标
position = str.find("you", 9);
cout << "str.find(you,9) is: " << position <<endl;
string substr = "i";
position = 0;
int i = 1;
while ((position = str.find_first_of(substr,position)) != string :: npos)
{
//查找 str 中substr出现的位置
cout << "position "<<i++<<" :" <<position <<endl;
position++;
}
string flag = "to";
position = str. rfind(flag); //反向查找flag 在 str 中最后出现的位置
cout << "str.rfind(flag) : " <<position <<endl;
getchar();
return 0;
}
7.除了string中的find函数外,char[]数组也有强大的查找函数
C++中有strstr、strchr等也有查找功能。函数说明如下
char *strstr( const char *str, const char *substr ); 返回指针,指向substr在字符串str中首次出现的位置。
char *strchr( const char *str, int ch ); 返回指针,指向 str 中字符ch 首次出现的位置。
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
char str[] = {"keep your decide in mind, not change except for your parents. "};
char *psubstr = "your";
char *p = strstr(str,psubstr); //如果没找到, 返回NULL
if (p)
{
cout << "找到了字符串, 下标为:" << p->str <<endl;
}
p = strchr(str, 'f');// 如果没找到 返回NULL
if (p)
{
cout << "找到了字符串, 从该字符开始的字符串为 " << p <<endl;
}
p = strrchr(str, 'e');// 如果没找到 返回NULL
if (p)
{
cout << "找到了字符串, 从该字符开始的字符串为 " << p <<endl;
}
getchar();
return 0;
}
8.与 char[ ] 的相互转换
copy(),返回指针,赋值给char[ ]数组名
c_str() ,返回 const 类型的指针
data() ,将内容以字符数组的形式返回
#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
using namespace std;
int main()
{
char strch[] = {"shrimp"};
string str = strch; //char[]可以直接赋值给string
cout<<str<<endl;
str = "fish";
strcpy(strch, str.c_str());
cout << strch <<endl;
str = "duck";
str.copy(strch,str.length(),0);//可指定拷贝的位置和字符的个数
cout << strch <<endl;
str = "chicken";
strcpy(strch, str.data()); //data()返回指向自己的第一个字符的指针
cout << strch <<endl;
getchar();
return 0;
}
9.与 char[ ] 的相互转换
copy(),返回指针,赋值给char[ ]数组名
c_str() ,返回 const 类型的指针
data() ,将内容以字符数组的形式返回
#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
using namespace std;
int main()
{
char str[] = "a,b, c,d*e flower";
const char*split = ",*"; //以【空格 ,*】三个字符来分割
char *p;
p = strtok (str, split);
while (p != NULL)
{
cout << p <<"\n";
p = strtok(NULL,split);
}
string city = "nanjingshanghai";
string strsh = city.substr(7,8); //提取子串
cout << strsh;
getchar();
return 0;
}
10.string 大小分配函数
capacity() ,返回容器在它已经分配的内存中可以容纳多少元素
resize(Container::size_type n),强制把容器改为容纳n个元素
#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
using namespace std;
int main()
{
string str = "spring-sumper-autumn-winter";
int isize = str.size(); //容器中有多少元素 27
int lenth = str.length(); //和size函数功能相同 27
int maxsize = str.max_size();//字符串可能的最大大小 1073741820
//在不重新分配内存的情况下,字符串可能的大小
int cpsize = str.capacity(); //容器在它已经分配的内存中可以容纳多少元素 27
if(str.empty()) //判断是否为空
{
cout << "字符串为空!";
}
//提前预留一定容量大小,避免多次容量扩充操作符导致效率低下
str.reserve();
str.resize(2014); //强制把容器改为容纳n个元素
cout<< str.length() << endl;
getchar();
return 0;
}
11. string 中的字符替换、删除操作
#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
string str = "standard lover";
string str2 = "gates of down";
str.swap(str2); //交换
cout << str << endl;
string strtemp = "OF";
str.replace (6,strtemp.length(), strtemp); //替换字符串 有多种形参格式
cout << str << endl;
str.erase(); //删除 index = 2, 长度为 4 的字符串
cout << str << endl;
str.erase(); //删除整个字符串
str2.clear(); //清空字符容器中所有的内容
getchar();
return 0;
}
了解更多关于string函数的用法:http://www.cplusplus.com/reference/string/string/append/