深入浅出之string
string 類是?STL?中 basic_string 模板實例化得到的模板類。其定義如下:
typedef basic_string <char> string;1. 定義及初始化string對象
string 類有多個構造函數,用法示例如下:
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開始的length個字符 如:string s4("12345", 1, 3); //s4 = "234",即 "12345" 的從下標 1 開始,長度為 3 的子串 5)string(const char *str,size_type index)字符串值為str第index開始的所有字符 如:string S1(“Strings”,8)注意:?string 類沒有接收一個整型參數或一個字符型參數的構造函數。下面的兩種寫法是錯誤的:
string s1('K'); string s2(123);2. 字符串賦值
2.1 assign函數?
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 賦值運算符=?
string類已經對“=”進行了重載,可以用 char* 類型的變量、常量,以及 char 類型的變量、常量對 string 對象進行賦值。例如:
string s1; s1 = "Hello"; // s1 = "Hello" s2 = 'K'; // s2 = "K”3. 字符串比較
?3.1 關系運算符
? ? ==,>,<,>=,<=,!=都已經重載,對字符串的操作可以適用。
?3.2 compare函數
?compare 成員函數有以下返回值:
- 小于 0 表示當前的字符串小;
- 等于 0 表示兩個字符串相等;
- 大于 0 表示另一個字符串小。
4. 字符串的連接
?4.1 append函數
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 =+符合賦值運算符?
string類已經將+運算符重載,可以對字符串進行連接
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 設置字符串長度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(); } 輸出結果: 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區別在于,at函數提供范圍檢測,越界時會拋出異常;而【】不會提供下標檢測。?
6.3? 獲得一個字符串 c_str
const char *c_str()const;
返回一個以null終止的字符串
字符數組轉化成string類型
char ch [] = “ABCDEFG”;
string str(ch);//也可string str = ch;
或者
char ch [] = “ABCDEFG”;
string str;
str = ch;//在原有基礎上添加可以用str += ch;
將string類型轉換為字符數組
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 對象的子串
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?交換兩個string對象的內容
swap 成員函數可以交換兩個 string 對象的內容。例如:
string s1("West”), s2("East"); s1.swap(s2); // s1 = "East",s2 = "West"?6.6?查找子串和字符
string 類有一些查找子串和字符的成員函數,它們的返回值都是子串或字符在 string 對象字符串中的位置(即下標)。如果查不到,則返回 string::npos。string: :npos 是在 string 類中定義的一個靜態常量。這些函數如下:
- find:從前往后查找子串或字符出現的位置。
- rfind:從后往前查找子串或字符出現的位置。
- find_first_of:從前往后查找何處出現另一個字符串中包含的字符。例如:
- s1.find_first_of("abc");? //查找s1中第一次出現"abc"中任一字符的位置
- find_last_of:從后往前查找何處出現另一個字符串中包含的字符。
- find_first_not_of:從前往后查找何處出現另一個字符串中沒有包含的字符。
- find_last_not_of:從后往前查找何處出現另一個字符串中沒有包含的字符。
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 個 '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); //刪除下標5及其后面的所有字符,此后 s1 = "R Ste"?6.9 插入子串
string s1("Limitless"), s2("00"); s1.insert(2, "123"); //在下標 2 處插入字符串"123",s1 = "Li123mitless" s1.insert(3, s2); //在下標 2 處插入 s2 , s1 = "Li10023mitless" s1.insert(3, 5, 'X'); //在下標 3 處插入 5 個 'X',s1 = "Li1XXXXX0023mitless"7. string與各類型轉換
待補充
參考資料
- http://c.biancheng.net/view/400.html
總結
以上是生活随笔為你收集整理的深入浅出之string的全部內容,希望文章能夠幫你解決所遇到的問題。