標準庫型別string表示可變長的字串行,使用string必須包含string標頭檔案。
1、定義和初始化#include
using
std::string;
2、string物件操作string s;//空字串s
string s1=s;//s1是s的副本
string s2="hello csdn blog";//s2是字串常量的副本
string s3(100,'u');//用100個'u'初始化
從cin中讀取string
3、處理string物件中的字元string str;
cin>>str;
cout<《讀取未知的string物件
string s;
while(cin>>s)//反覆讀取直到檔案末尾
cout
《使用getline讀取一整行
string line;
while(getline(cin,line))
if(!line.empty())
//每次讀取一整行,遇到空行直接跳過
cout<《兩個string物件可以進行比較,賦值,相加的操作。也可以將string於常量字串相加
string s1=s+"we are you";
使用庫函式處理string字元
輸出string中的每個字元```
#include
isallnum(c);//檢測數字
isalpha(c);//檢測字母
isdigit(c);//檢測數字
islower(c);//檢測小寫字母
isounct(c);//檢測標點符號
tolower(c);//全轉換為小寫字母
toupper(c);//全轉換為大寫字母tring中的每個字元
//c++11方法
for(auto c:str)
cout<< c for(int i=0;icout<
C 中String用法的簡單總結
1.定義和初始化 string s1 string s1 s2 string s1 abc string s1 n,x s1為x的n個副本 string s1 s2,pos,len s1被初始化成s2中從pos開始的len個字元的副本。2.讀寫 cin s 忽略開頭空白字元,到遇到空白字元為止 co...
標準C 中string類的用法總結
相信使用過mfc程式設計的朋友對cstring這個類的印象應該非常深刻吧?的確,mfc中的cstring類使用起來真的非常的方便好用。但是如果離開了mfc框架,還有沒有這樣使用起來非常方便的類呢?答案是肯定的。也許有人會說,即使不用mfc框架,也可以想辦法使用mfc中的api,具體的操作方法在本文最...
c 之string類用法詳細總結
標準c 中string類非常強大,合理使用,能極大提高程式設計效率,下面就對string類的用法進行總結。標頭檔案 include 1 string s 生成乙個空字串s 2 string s str 拷貝建構函式生成str的複製品 3 string s str,index 將字串str內 始於位置...