1.輸入輸出
str.lengh()/str.size() 兩個函數都可以取得string的長度,元素個數
直接cin cout輸入輸出
非要用printf()輸出得str.c_str() 轉換成c語言中的字元數組
#include<iostream>
#include <cstdio>
#include<string>
using namespace std;
int main(){
string str;
cin>>str;
printf("%s\n", str.c_str());//c_str()轉換為字元數組
return 0;
}
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI0gTMx81dsQWZ4lmZf1GLlpXazVmcvwFciV2dsQXYtJ3bm9CX9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCMy81dvRWYoNHLwEzX5xCMx8FesU2cfdGLwMzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5SO4AzM5EGZiZDMjlzYxgjMzYzX4ATOzUTM5EzLclDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.png)
2.疊代器通路
有些api必須iterator類型參數
注意string::iterator it;//沒有<>
#include<iostream>
#include<string>
using namespace std;
int main(){
string str="abcdef";
string::iterator it;
for(it=str.begin();it!=str.end();it++){
printf("%c", *it);
}
return 0;
}
3.+ > < == <= >= !=
可以直接用
4.insert
insert(pos,string) 在pos處(pos下标左邊)插入字元串string
#include<iostream>
#include<string>
using namespace std;
int main(){
string str1="abcxyz",str2="123";
str1.insert(3,str2);
cout<<str1<<endl;
return 0;
}
insert(it,it1,it2)
在it處(it疊代器所指位置左邊)插入字元串[it1,it2)區間内的字元串
#include<iostream>
#include<string>
using namespace std;
int main(){
string str1="abcxyz",str2="123";
str1.insert(str1.begin()+3,str2.begin()+1,str2.end());
cout<<str1<<endl;
return 0;
}
5.erase删除
1.删除單個元素
str.erase(it) 删除疊代器it所指位置的元素
2.删除區間元素
str.erase(first,end); 删除疊代器區間[first,end)的所有元素
str.erase(index,length); 删除index下标開始,後面數共length個字元 ★★
#include<iostream>
#include<string>
using namespace std;
int main(){
string str="hello2world";
cout<<"原始:"<<str<<endl;
str.erase(str.begin()+5);
cout<<"删除下标5後:"<<str<<endl;
str.erase(str.begin(),str.begin()+5);
cout<<str<<endl;
str.erase(1,3);
cout<<"下标1開始往後删除3個:"<<str<<endl;
return 0;
}
6.clear()清空string中的資料,O(1)
7.substr()
substr(pos,len)傳回pos下标開始,長度為length的子串
substr(pos) 傳回pos下标開始(包含pos)後面所有元素
#include<iostream>
#include<string>
using namespace std;
int main(){
string str="Thank you for your smile.";
cout<<str.substr(0,5)<<endl;
cout<<str.substr(14,4)<<endl;
cout<<str.substr(19,5)<<endl;
cout<<str.substr(5);//5下标開始 截取後面所有元素
return 0;
}
8.string::npos
string::npos unsigned_int類型的-1,作為find函數失敗時的傳回值,即沒有找到
值為-1或者4294967295=2^32-1 unsigned類型最大值
#include<iostream>
#include <string>
using namespace std;
int main(){
cout<<(string::npos==-1)<<endl;
cout<<(string::npos==4294967295)<<endl;
return 0;
}
都傳回true
9.find
str.find(str2) str2是str子串時傳回第一次出現的位置(下标),未找到傳回string::npos
str.find(str2,n) 從下标n處開始找,傳回值同上
str2可以是字元串 也可以是單個字元
#include<iostream>
#include <string>
using namespace std;
int main(){
string str="Thank you for your smile";
string str2="you";string str3="me";
char c1='a',c2='b';
if(str.find(str2)!=string::npos){
cout<<str.find(str2)<<endl;
}
if(str.find(str2,7)!=string::npos){
cout<<str.find(str2,7)<<endl;
}
if(str.find(str3)!=string::npos){
cout<<"找到\n";
}else{
cout<<"未找到\n";
}
//字元也行
cout<<"c1:"<<str.find(c1)<<endl;
cout<<"c2:"<<(int)str.find(c2)<<endl;
return 0;
}
10.replace()
replace(s,len,str); 下标為s開始,後面len個字元替換成str
#include<iostream>
#include<string>
using namespace std;
int main(){
string str="Maybe you will turn around.";
string str2="will not";
string str3="surely";
cout<<str.replace(10,4,str2)<<endl;//will換成will not
cout<<str.replace(str.begin(),str.begin()+5,str3)<<endl;//Maybe換成surely
cout<<str<<endl;//此API影響str本身
return 0;
}
補充:
進階查找:
str.find_first_of("xyz"); 傳回字元串str中第一個和"xyz"字元串中任意一個字元比對的字元的下标
str.find_first_not_of("xyz"); 傳回字元串str中第一個和"xyz"字元串中任意一個字元都不比對的字元的下标
作用一樣,從後往前找
str.find_last_of("xyz");
str.find_last_not_of("xyz");
#include<iostream>
#include<string>
using namespace std;
int main(){
string s="123abc";
cout<<s.find_first_of("efga")<<endl;
cout<<s.find_first_not_of("321")<<endl;
cout<<s.find_first_not_of("321a")<<endl;
return 0;
}
string轉char string.c_str()
char轉string 直接指派 String=str