深入浅出之string
string 類是?STL?中 basic_string 模板實(shí)例化得到的模板類。其定義如下:
typedef basic_string <char> string;1. 定義及初始化string對(duì)象
string 類有多個(gè)構(gòu)造函數(shù),用法示例如下:
1) string()字符串為空值 如:string s1(); // si = "" 2)string(const char *str) 字符串為str中的字符 如:string s2("Hello"); // s2 = "Hello" 3)string(size_type length,char ch) 如:string s3(4, 'K'); // s3 = "KKKK" 4)string(const char *str,size_type index,size_type length) 字符串值為str以index開(kāi)始的length個(gè)字符 如:string s4("12345", 1, 3); //s4 = "234",即 "12345" 的從下標(biāo) 1 開(kāi)始,長(zhǎng)度為 3 的子串 5)string(const char *str,size_type index)字符串值為str第index開(kāi)始的所有字符 如:string S1(“Strings”,8)注意:?string 類沒(méi)有接收一個(gè)整型參數(shù)或一個(gè)字符型參數(shù)的構(gòu)造函數(shù)。下面的兩種寫(xiě)法是錯(cuò)誤的:
string s1('K'); string s2(123);2. 字符串賦值
2.1 assign函數(shù)?
string s1("12345"), s2; s3.assign(s1); // s3 = s1 s2.assign(s1, 1, 2); // s2 = "23",即 s1 的子串(1, 2) s2.assign(4, 'K'); // s2 = "KKKK" s2.assign("abcde", 2, 3); // s2 = "cde",即 "abcde" 的子串(2, 3)2.2 賦值運(yùn)算符=?
string類已經(jīng)對(duì)“=”進(jìn)行了重載,可以用 char* 類型的變量、常量,以及 char 類型的變量、常量對(duì) string 對(duì)象進(jìn)行賦值。例如:
string s1; s1 = "Hello"; // s1 = "Hello" s2 = 'K'; // s2 = "K”3. 字符串比較
?3.1 關(guān)系運(yùn)算符
? ? ==,>,<,>=,<=,!=都已經(jīng)重載,對(duì)字符串的操作可以適用。
?3.2 compare函數(shù)
?compare 成員函數(shù)有以下返回值:
- 小于 0 表示當(dāng)前的字符串小;
- 等于 0 表示兩個(gè)字符串相等;
- 大于 0 表示另一個(gè)字符串小。
4. 字符串的連接
?4.1 append函數(shù)
string s1("123"), s2("abc"); s1.append(s2); // s1 = "123abc" s1.append(s2, 1, 2); // s1 = "123abcbc" s1.append(3, 'K'); // s1 = "123abcbcKKK" s1.append("ABCDE", 2, 3); // s1 = "123abcbcKKKCDE",添加 "ABCDE" 的子串(2, 3)4.2 =+符合賦值運(yùn)算符?
string類已經(jīng)將+運(yùn)算符重載,可以對(duì)字符串進(jìn)行連接
string s1(“hello”),s2(“world”); s1 = s1+s2;5. 字符串特性
5.1 獲取字符串大小size或length
#include <QCoreApplication> #include <string> #include <iostream>using namespace std;int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);string s,s1,s2;s1 = "hello";s2 = "world";s = s1+" "+s2;qDebug("%s-%d-%d",s.c_str(),s.length(),s.size());return a.exec(); }5.2 獲取字符容量
int capacity() const;
5.3 設(shè)置字符串長(zhǎng)度resize
void resize(int len, char c);?
#include <QCoreApplication> #include <string> #include <iostream>using namespace std;int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);string s,s1,s2;s1 = "hello";s2 = "world";s = s1+" "+s2;s.resize(12,'1');qDebug("%s-%d-%d",s.c_str(),s.length(),s.size());return a.exec(); } 輸出結(jié)果: hello world1-12-126. 字符串操作?
6.1 取得字符串中的字符 []
char& operator[](int n)
string s("hello world"); char c = s[1];?6.2 取得字符串中的字符 at
char& at(int n)
string s("hello world"); char c = s.at(1);注意:
【】和at區(qū)別在于,at函數(shù)提供范圍檢測(cè),越界時(shí)會(huì)拋出異常;而【】不會(huì)提供下標(biāo)檢測(cè)。?
6.3? 獲得一個(gè)字符串 c_str
const char *c_str()const;
返回一個(gè)以null終止的字符串
字符數(shù)組轉(zhuǎn)化成string類型
char ch [] = “ABCDEFG”;
string str(ch);//也可string str = ch;
或者
char ch [] = “ABCDEFG”;
string str;
str = ch;//在原有基礎(chǔ)上添加可以用str += ch;
將string類型轉(zhuǎn)換為字符數(shù)組
char buf[10];
string str(“ABCDEFG”);
length = str.copy(buf, 9);
buf[length] = ‘\0’;
或者
char buf[10];
string str(“ABCDEFG”);
strcpy(buf, str.c_str());//strncpy(buf, str.c_str(), 10);
6.4?求 string 對(duì)象的子串
string substr(int n = 0, int m = string::npos) const;
string s1 = "this is ok"; string s2 = s1.substr(2, 4); // s2 = "is i" s2 = s1.substr(2); // s2 = "is is ok"6.5?交換兩個(gè)string對(duì)象的內(nèi)容
swap 成員函數(shù)可以交換兩個(gè) string 對(duì)象的內(nèi)容。例如:
string s1("West”), s2("East"); s1.swap(s2); // s1 = "East",s2 = "West"?6.6?查找子串和字符
string 類有一些查找子串和字符的成員函數(shù),它們的返回值都是子串或字符在 string 對(duì)象字符串中的位置(即下標(biāo))。如果查不到,則返回 string::npos。string: :npos 是在 string 類中定義的一個(gè)靜態(tài)常量。這些函數(shù)如下:
- find:從前往后查找子串或字符出現(xiàn)的位置。
- rfind:從后往前查找子串或字符出現(xiàn)的位置。
- find_first_of:從前往后查找何處出現(xiàn)另一個(gè)字符串中包含的字符。例如:
- s1.find_first_of("abc");? //查找s1中第一次出現(xiàn)"abc"中任一字符的位置
- find_last_of:從后往前查找何處出現(xiàn)另一個(gè)字符串中包含的字符。
- find_first_not_of:從前往后查找何處出現(xiàn)另一個(gè)字符串中沒(méi)有包含的字符。
- find_last_not_of:從后往前查找何處出現(xiàn)另一個(gè)字符串中沒(méi)有包含的字符。
6.7?替換子串
string s1("Real Steel"); s1.replace(1, 3, "123456", 2, 4); //用 "123456" 的子串(2,4) 替換 s1 的子串(1,3) cout << s1 << endl; //輸出 R3456 Steel string s2("Harry Potter"); s2.replace(2, 3, 5, '0'); //用 5 個(gè) '0' 替換子串(2,3) cout << s2 << endl; //輸出 HaOOOOO Potter int n = s2.find("OOOOO"); //查找子串 "00000" 的位置,n=2 s2.replace(n, 5, "XXX"); //將子串(n,5)替換為"XXX" cout << s2 < < endl; //輸出 HaXXX Potter6.8 刪除子串?
string s1("Real Steel"); s1.erase(1, 3); //刪除子串(1, 3),此后 s1 = "R Steel" s1.erase(5); //刪除下標(biāo)5及其后面的所有字符,此后 s1 = "R Ste"?6.9 插入子串
string s1("Limitless"), s2("00"); s1.insert(2, "123"); //在下標(biāo) 2 處插入字符串"123",s1 = "Li123mitless" s1.insert(3, s2); //在下標(biāo) 2 處插入 s2 , s1 = "Li10023mitless" s1.insert(3, 5, 'X'); //在下標(biāo) 3 處插入 5 個(gè) 'X',s1 = "Li1XXXXX0023mitless"7. string與各類型轉(zhuǎn)換
待補(bǔ)充
參考資料
- http://c.biancheng.net/view/400.html
總結(jié)
以上是生活随笔為你收集整理的深入浅出之string的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。