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'以外的所有字符串,包括'\n'和空格
如果想反復讀入字符
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/
?