C++ String常規操作

1、獲得長度

int len = s.length();

2、連接字符串

s = s1 + s2;

3、比較字符串

s1 < s2

4、倒置串

reverse(s.begin(), s.end());

5、字符串轉字符數組

char* c = new char[30];

strcpy(c, s.c_str());

6、查找串

(1)int pos = s1.find(s2, pos); // 在s1中找s2,如果找到返回第一次出現的位置,如果沒找到返回-1

最標準的寫法:string::size_type index = s1.find(s2, pos);

如何判斷是否找到了:if (index != s1.npos)

(2)int index =  s1.find(s2, pos, len);// 從s1的pos位置開始,匹配s2的前len個字符參與匹配

(3)int index = s1.rfind(s2, pos);// 從pos位置開始向前查找。

(4)int index = s1.find_first_of(s2, pos);// 從pos開始,找出第一個(s1中的字符在s2串裏面)元素

(5)int index = s1.find_last_of(s2, pos);// 從指定位置往前找

(6)int index = s1.find_first_not_of(s2, pos); // 如果找到第一個不在s2中的就返回

(7)int index = s1.find_last_not_of(s2, pos); // 從指定位置往前找

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章