C++标准库之String

C++中支持的字符串处理的函数库叫String,但它不是STL,却与STL操作十分相似。

1.声明:

使用String之前要有以下头文件

#include<string>
using namespace std;

声明方法

string s;   //声明一个string对象 s
string s[10]; //声明一个string对象数组s 

初始化string对象

直接初始化:利用小括号完成;

拷贝初始化:利用等号完成拷贝过程(减少使用);

string s1;            //定义一个空字符 
string s2 = s1;       //拷贝初始化,将s1赋值给s2 
string s3 = "Hello";  //拷贝初始化,用字符串字面值初始化s3 
string s4(10,'c');    //直接初始化,s4内容是cccccccccc 
string s5(s3);        //直接初始化,这是新的初始化方法,等价于s5 = s3 
string s6("World");      //直接初始化,这是新的初始化方法,等价于s6 = “World”

以下是运行结果

//
//
//Hello
//cccccccccc
//Hello
//World

输入字符串方法

cin >> s7;          // 读取有效字符串直到遇到空格 
getline(cin,s8);    // 读取字符串直到遇到换行结束,可读入空格 
getline(cin,s9,'a') // 读取除了'a'以外的所有字符串,包括'
'和空格 

如果想反复读入字符

string s10;
while(cin >> s10){
    //其他操作 
}

2.string对象的操作

s.empty();                  //   判断是否为空,返回bool型 
s.size(),s.length();        //   返回字符串的个数 
s[n];                       //   字符串第n-1个字符,从0开始计数 
s1+s2;                      //   将s2字符串连接在s1后面 
    s = s + '0';            //   在s字符串后面加个字符0 
    s = s + 'a'             //   在s字符串后面加个字符a 
    string s2 = s1 + "adc"  //   s1字符串后加个字符串"abc"赋值给s2 
s1 = s2;                    //   将s2字符串赋值给s1 
s1 == s2;                   //   判断s2字符串和s1字符串是否相等
!=,<,<=,>,>=                //   两个字符串的比较

3.string对象中字符的处理(头文件cctype

isalnum(c);   //如果c是字母或者数字,返回true 
isalpha(c);   //如果c是字母,返回true 
iscntrl(c);   //如果c是控制字符,返回true 
isdigit(c);   //如果c是数字,返回true 
isgraph(c);   //如果c不是空格,可打印,返回true 
islower(c);   //如果c是小写字母,返回true 
isupper(c);   //如果c是大写字母,返回true
isprint(c);   //如果c是可打印字母,返回true
ispunct(c);   //如果c是标点符号,返回true
isspace(c);   //如果c是空白字符,返回true
isxdigit(c);  //如果c是十六进制数,返回true
tolower(c) ;  //如果c是大写字母,返回小写字母 
toupper(c);   //如果c是小写字母,返回大写字母

4.函数操作

1)assign():

s.assign(base);                           //将base赋值给s 
s.assign(base,10,9);                      //将base第10字符已经后9个字符赋值给s 
s.assign("you are so beautiful",7);       //将字符串中前0-6个字符赋值给s 
s.assign("you are so beautiful");         //将字符赋值给s 
s.assign(10,'*');                         //将10个*赋值给s 
s.assign<int>(10,0x2D);                   //赋值10个-给s 
s.assign(base.begin()+16,base.end()-12);  //将base迭代器中指定位置的字符赋给s 

2)insert():     

  string str="to be question";
  string str2="the ";
  string str3="or not to be";
  string::iterator it;

  // used in the same order as described above:
  //从0开始计数 
  str.insert(6,str2);                            // 将字符串str2插入到str的第六个位置(str[5]) 
  str.insert(6,str3,3,4);                        // 将str3第3,4,5,6四个字符插入到str第六个位置处 
  str.insert(10,"that is cool",8);               // 将"that is cool"字符串中前八个字符插入到str第10个位置处 
  str.insert(10,"to be ");                       // 将"to be "插入到str第10个位置处 
  str.insert(15,1,':');                          //  插入一个,到str的第15个位置处 
  it = str.insert(str.begin()+5,',');            //  将,插入到字符串开头的第五个位置  
  cout << *it << endl;                           //it指向的是, 
  str.insert (str.end(),3,'.');                  //  在字符串末尾插入3个. 
  str.insert (it+2,str3.begin(),str3.begin()+3); //将str3前3个字符插入到,后面的第2个位置 

3)find():                                                            //字符串查找,若存在返回下标。

4)replace():                                                      //替换字符

5)erase():                                                        //删除字符

6)append(),+=,push_back():                       //在尾部添加字符

7)compare(),==,!=,<,<=,>,>=:        //字符串的比较

8)reverse():                                                  //保留一定量内存以容纳一定数量的字符

9)substr():                                                    //返回某个字符串a) 

10) swap()                                                        //交换两个字符串的内容

11) clear()                                                       //删除全部字符 

12) +                                                               //串联字符串

13) size(),length()                                          //返回字符数量

14) max_size()                                              //返回字符的可能最大个数

15) empty()                                                   //判断字符串是否为空

16) capacity()                                               //返回重新分配之前的字符容量

17) [ ], at()                                                    //存取单一字符

18) >>,getline()                                            //从stream读取某值

19) <<                                                          //将值写入stream

20) copy()                                                   //将某值赋值为一个C_string

21) c_str()                                                  //将内容以C_string返回

22) data()                                                  //将内容以字符数组形式返回

23) substr()                                               //返回某个子字符串

24)begin() end()                                      //提供类似STL的迭代器支持

25) rbegin() rend()                                  //逆向迭代器

26) get_allocator()                                  //返回配置器

(未完待续)(以后需要什么再查官方文档)
http://www.cplusplus.com/reference/string/string/

原文地址:https://www.cnblogs.com/wanghao-boke/p/10440836.html