C++字串操作
這次要介紹關於字串的特殊用法,會介紹插入、刪除、修改、比對、取子字串、搜尋、取代、取字元陣列在排序。
那我們一一介紹吧~~
我先說一下,之後會出現一個叫string::npos的東西,它是用來跑從字串頭到字串尾的東西,表示字串的結尾!!
插入:
- int main()
- {
- string s, target = "book", ins = "the ";
- getline(cin,s);
- int i,pos = 0;
- while((i=s.find(target,pos))!=string::npos)
- {
- s.insert(i,ins);
- pos = i + ins.size()+1;
- }
- cout << s << endl;
- }
插前插後你就自己想想看啦~~
刪除:
- int main()
- {
- string s,ins = "do";
- getline(cin,s);
- int i,pos = 0;
- while((i=s.find(ins,pos))!=string::npos)
- {
- s.erase(i,ins.size());
- pos = i + ins.size()+1;
- }
- cout << s << endl;
- }
修改:
- int main()
- {
- string s = string();
- s = "york ";
- cout << s << endl;
- s += "is a boy";
- cout << s << endl;
-
- s.assign("life is beautiful",8,9);
- cout << s <<endl;
- s.append(" islander",7);
- cout << s << endl;
- }
比對:
- int main()
- {
- string s1 = "york";
- string s2 = "york";
- string s3 = "yorkk";
- cout << s1.compare(s2) << endl;
- cout << s1.compare(s3) << endl;
- cout << s1.compare(0,2,s1)<< endl;
- cout << s3.compare(0,4,s1)<< endl;
- }
取子字串:
- int main()
- {
- string s = "yorkisaboy";
- cout << s.substr(0,4) << " ";
- cout << s.substr(4,2) << " ";
- cout << s.substr(6,1) << " ";
- cout << s.substr(7,3) << " ";
- }
搜尋 (find()--->左至右搜尋,rfind()----->右至左搜尋):
- int main()
-
- {
-
- string s = "york is a boy",want;
-
- cout << "請輸入要找的字";
-
- cin >> want;
-
- int i,count = 0,pos = 0;
-
- while((i=s.find(want,pos))!=string::npos)
- {
-
- count++;
-
- cout << "第 " << count << " 次出現[" << want << "]的位置在第 "
-
- << i+1 << " 個字" << endl;
-
- pos = i+1;
-
- }
-
- if(count == 0)
-
- cout << "沒符合["<<want << "]的字串";
-
- else
-
- cout << "總共找到 "<< count << " 次";
- }
- 數字(find_first_of----->有出現的任一元素
- find_last_of------>除了這些其他都是):
- 例如:find_first_of("abc"),則表示找a或b或c;
- find_last_of及find_first_not_of則反之
- int main()
-
- {
-
- string s ,target="0123456789";
-
- cout << "請輸入一個字串:";
-
- getline(cin,s);
-
-
- int i,count = 0,pos = 0;
-
- while((i=s.find_first_of(target,pos))!=string::npos)
-
- {
-
- count++;
-
- pos = i+1;
-
- }
-
- cout << endl << "在["<< s << "]中";
-
- if(count == 0)
-
- cout << "沒有數字字元!!";
-
- else
-
- cout << "共有 "<< count << " 個數字字元";
- }
取代:
- int main()
- {
- string s,s1,s2;
- cout << "請輸入一個字串:";
- getline(cin,s);
- cout << "請輸入要替換的字串:";
- getline(cin,s1);
- cout << "要將["<< s1 << "]換成?";
- getline(cin,s2);
- int i,pos = 0;
- unsigned int len1= s1.size(),len2 = s2.size();
- while((i=s.find(s1,pos))!=string::npos)
- {
- s.replace(i,len1,s2);
- pos = i + len2 + 1;
- }
- cout << "新字串:"<< s;
- }
取字元陣列在排序:
- int main()
- {
- string s;
- cout << "請輸入一個字串:";
- cin >> s;
- int len = s.size();
- char *cstr = new char(len+1);
- strcpy(cstr,s.c_str());
- for(int i=0;i<len;i++)
- {
- for(int j=i+1;j<len;j++)
- {
- if(cstr[i]>cstr[j])
- {
- char tmp = cstr[i];
- cstr[i] = cstr[j];
- cstr[j] = tmp;
- }
- }
- }
- cout << "將字串內容排序後:"<<cstr;
- delete [] cstr;
- }
其實功能是可以混著用的,蠻多練習題目都是將他們活用,這樣就能省很多程式碼喔
END