<string>,下面的例子介紹了幾種定義 string 變量(對象)的方法:
#include <iostream>
#include <string>
using namespace std;
int main(){
string s1;
string s2 = "c plus plus";
string s3 = s2;
string s4 (5, 's');
return 0;
}
變量 s1 只是定義但沒有初始化,編譯器會將默認值賦給 s1,默認值是"",也即空字符串。"c plus plus"。與C風格的字符串不同,string 的結尾沒有結束標志'\0'。"c plus plus"。's'字符組成的字符串,也就是"sssss"。=進行賦值。string 變量也可以用C風格的字符串進行賦值,例如,s2 是用一個字符串常量進行初始化的,而 s3 則是通過 s2 變量進行初始化的。string s = "http://c.biancheng.net"; int len = s.length(); cout<<len<<endl;輸出結果為
22。由於 string 的末尾沒有'\0'字符,所以 length() 返回的是字符串的真實長度,而不是長度 +1。string path = "D:\\demo.txt"; FILE *fp = fopen(path.c_str(), "rt");為了使用C語言中的 fopen() 函數打開文件,必須將 string 字符串轉換為C風格的字符串。
>>進行輸入,用<<進行輸出。請看下面的代碼:
#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
cin>>s; //輸入字符串
cout<<s<<endl; //輸出字符串
return 0;
}
運行結果:>>默認會忽略空格,遇到空格就認為輸入結束,所以最後輸入的http://vip.biancheng.net沒有被存儲到變量 s。
#include <iostream>
#include <string>
using namespace std;
int main(){
string s = "1234567890";
for(int i=0,len=s.length(); i<len; i++){
cout<<s[i]<<" ";
}
cout<<endl;
s[5] = '5';
cout<<s<<endl;
return 0;
}
運行結果:s[5] = '5';就將第6個字符修改為 '5',所以 s 最後為 "1234557890"。+或+=運算符來直接拼接字符串,非常方便,再也不需要使用C語言中的 strcat()、strcpy()、malloc() 等函數來拼接字符串了,再也不用擔心空間不夠會溢出了。+來拼接字符串時,運算符的兩邊可以都是 string 字符串,也可以是一個 string 字符串和一個C風格的字符串,還可以是一個 string 字符串和一個字符數組,或者是一個 string 字符串和一個單獨的字符。請看下面的例子:
#include <iostream>
#include <string>
using namespace std;
int main(){
string s1 = "first ";
string s2 = "second ";
char *s3 = "third ";
char s4[] = "fourth ";
char ch = '@';
string s5 = s1 + s2;
string s6 = s1 + s3;
string s7 = s1 + s4;
string s8 = s1 + ch;
cout<<s5<<endl<<s6<<endl<<s7<<endl<<s8<<endl;
return 0;
}
運行結果:string& insert (size_t pos, const string& str);
pos 表示要插入的位置,也就是下標;str 表示要插入的字符串,它可以是 string 字符串,也可以是C風格的字符串。
#include <iostream>
#include <string>
using namespace std;
int main(){
string s1, s2, s3;
s1 = s2 = "1234567890";
s3 = "aaa";
s1.insert(5, s3);
cout<< s1 <<endl;
s2.insert(5, "bbb");
cout<< s2 <<endl;
return 0;
}
運行結果:string& erase (size_t pos = 0, size_t len = npos);
pos 表示要刪除的子字符串的起始下標,len 表示要刪除子字符串的長度。如果不指明 len 的話,那麼直接刪除從 pos 到字符串結束處的所有字符(此時 len = str.length - pos)。
#include <iostream>
#include <string>
using namespace std;
int main(){
string s1, s2, s3;
s1 = s2 = s3 = "1234567890";
s2.erase(5);
s3.erase(5, 3);
cout<< s1 <<endl;
cout<< s2 <<endl;
cout<< s3 <<endl;
return 0;
}
運行結果:string substr (size_t pos = 0, size_t len = npos) const;
pos 為要提取的子字符串的起始下標,len 為要提取的子字符串的長度。
#include <iostream>
#include <string>
using namespace std;
int main(){
string s1 = "first second third";
string s2;
s2 = s1.substr(6, 6);
cout<< s1 <<endl;
cout<< s2 <<endl;
return 0;
}
運行結果:
size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
#include <iostream>
#include <string>
using namespace std;
int main(){
string s1 = "first second third";
string s2 = "second";
int index = s1.find(s2,5);
if(index < s1.length())
cout<<"Found at index : "<< index <<endl;
else
cout<<"Not found"<<endl;
return 0;
}
運行結果:
#include <iostream>
#include <string>
using namespace std;
int main(){
string s1 = "first second third";
string s2 = "second";
int index = s1.rfind(s2,6);
if(index < s1.length())
cout<<"Found at index : "<< index <<endl;
else
cout<<"Not found"<<endl;
return 0;
}
運行結果:
#include <iostream>
#include <string>
using namespace std;
int main(){
string s1 = "first second second third";
string s2 = "asecond";
int index = s1.find_first_of(s2);
if(index < s1.length())
cout<<"Found at index : "<< index <<endl;
else
cout<<"Not found"<<endl;
return 0;
}
運行結果: