0.append
用來在字符串的結尾增加字符串.也可以使用’+’運算符直接增加字符串.
string str = "abcde";
str.append("ab");
cout << str << endl;
//abcdeab
string str = "abcde";
str += "ab";
cout << str << endl;
//abcdeab
1.assign
assign方法用來重新給字符串賦值.
string str = "abcde";
str.assign(2,'a');
cout << str << endl;
//aa
//取指定字符串下標爲2開始,2個位置的字符.
string str = "abcde";
str.assign("asdasd",2,2);
cout << str << endl;
//da
//取指定字符串的前四個字符.
string str = "abcde";
str.assign("asdasd",4);
cout << str << endl;
//asda
2.at
獲取指定下標的字符.如果越界.則發出abort信號,中止程序.和[]一樣.不過’[]’越界直接提示越界.
string str = "abcde";
cout << str.at(1) << endl;
//b
string str = "abcde";
cout << str[1] << endl;
//b
3.back
返回字符串的最後一個字符.如果字符串爲空,則提示迭代器偏移量越界.訪問了非法內容.
string str = "abcde";
cout << str.back() << endl;
//e
4.front
返回字符串的第一個字符.如果字符串爲空,則提示迭代器偏移量越界.訪問了非法內容.
string str = "abcde";
cout << str.front() << endl;
//a
5.begin
返回了字符串的首迭代器,迭代器可以理解爲指針,也就是指向第一個字符的指針.但是迭代器和指針卻不能化爲一談.只能說用法類似.
string str = "abcde";
cout << *str.begin() << endl;
//a
6.end
返回了字符串的尾迭代器,尾迭代器指向字符串最後一個字符的後一個位置.
string str = "abcde";
cout << *--str.end() << endl;
//e
7.rbegin
逆向返回指向第一個字符的迭代器.
string str = "abcde";
cout << *str.rbegin() << endl;
//e
8.rend
逆向返回指向最後一個字符的下一個字符的迭代器.
string str = "abcde";
cout << *--str.rend() << endl;
//a
9.cbegin
返回指向字符串第一個字符的常量迭代器.不能修改迭代器所指向內容的值.
string str = "abcde";
//錯誤,返回的是常量迭代器.不能修改迭代器所指向內容的值.
//*str.cbegin() = 'w';
10.cend
返回指向字符串最後一個字符後一個字符的常量迭代器.不能修改迭代器所指向內容的值.
string str = "abcde";
//錯誤,返回的是常量迭代器.不能修改迭代器所指向內容的值.
//*--str.cend() = 'w';
11.clear
清空字符串的內容.
12.compare
比較字符串的大小.可以取出子串進行比較.
string str = "abcde";
cout << str.compare("qezx") << endl;
//-1
//比較的是"bc"和"ez"兩個子串的大小.
string str = "abcde";
cout << str.compare(1,2,"qezx",1,2) << endl;
//-1
13.c_str
把string類型轉化爲const char*類型.轉化爲了C風格的字符串,以’\0’結尾.
string str = "abcde";
char buff[16];
strcpy(buff,str.c_str());
cout << buff << endl;
//abcde.
14.data
與c_str()類似,但是返回的數組不以空字符終止.
15.empty
判斷字符串是否爲空.
16.erase
刪除字符串中指定的元素.
string str = "abcde";
str.erase(1,2);
cout << str << endl;
//ade
string str = "abcde";
str.erase(1);
cout << str << endl;
//a
string str = "abcde";
str.erase(str.begin(),str.begin()+2);
cout << str << endl;
//cde
17.find
返回從指定位置,順序查找第一次出現指定字符串的下標.
string str = "abacde";
cout << str.find('a') << endl;
//0
string str = "abacde";
cout << str.find('a',2) << endl;
//2
18.find_first_not_of
返回從指定位置,順序查找第一個不是指定字符串的下標.
string str = "abacde";
cout << str.find_first_not_of('a') << endl;
//1
19.find_first_of
返回字符串中第一個含有指定字符串中字符的下標.
string str = "abacde";
cout << str.find_first_of("wex") << endl;
//5
``
20.find_last_not_of
逆向查找,返回字符串中第一個不是指定字符串中任意字符的下標.
string str = "abacde";
cout << str.find_last_not_of("wex") << endl;
//4
21.find_last_of
逆向查找,返回字符串中第一個是指定字符串中任意字符的下標.
string str = "abacde";
cout << str.find_last_of("wex") << endl;
//5
22.insert
在指定位置插入字符或字符串.
string str = "abacde";
str.insert(2,"asd");
cout << str << endl;
//abasdacde
23.length
返回字符串的長度.
string str = "abacde";
cout << str.length() << endl;
//6
24.npos
返回字符串最後一個字符的下一個字符的位置.
string str = "abacde";
cout << (int)str.npos << endl;
//-1
25.max_size
返回字符串的最大長度.
string str = "abacde";
cout << str.max_size() << endl;
26.capacity
返回當前字符串的容量.
string str = "abacde";
cout << str.capacity() << endl;
//15
27.pop_back
刪除最後一個字符.
string str = "abacde";
str.pop_back();
cout << str << endl;
//abacd
28.push_back
在字符串末尾添加一個字符.
string str = "abacde";
str.pop_back('q');
cout << str << endl;
//abacdeq
29.replace
replace用來替換指定位置的字符串.
string str = "abcde";
str.replace(0,2,"asd");
cout << str << endl;
//asdcde
string str = "abcde";
str.replace(str.begin(),str.begin()+1,"zx");
cout << str << endl;
//zxbcde
30.reserve
調整字符串的內存大小,從而改變了容量的大小.
string str = "abcde";
str.reserve(20);
cout << str << endl;
cout << str.capacity() << endl;
//abcde
//31
31.resize
resize和reserve都可以改變容量的大小,但是resize可以控制字符的個數.減少則刪除元素,增加則自動填充指定字符,如果沒有指定,則添加’\0’.
string str = "abcde";
str.resize(2);
cout << str << endl;
cout << str.capacity() << endl;
//ab
//15
string str = "abcde";
str.resize(10,'z');
cout << str << endl;
//abcdezzzzz
32.size
返回字符串的長度,相當於C風格字符串的strlen方法.
string str = "abcde";
cout << str.size() << endl;
//5
33.substr
獲取字符串的子串.
string str = "abcde";
cout << str.substr(0) << endl;
cout << str.substr(2, 2) << endl;
//abcde
//cd
34.swap
用來和另外一個字符串進行交換.
string str = "abcde";
string temp = "as";
str.swap(temp);
cout << str << endl;
cout << temp << endl;
//as
//abcde