close
C++字串操作
這次要介紹關於字串的特殊用法,會介紹插入、刪除、修改、比對、取子字串、搜尋、取代、取字元陣列在排序。
 
那我們一一介紹吧~~
 
我先說一下,之後會出現一個叫string::npos的東西,它是用來跑從字串頭到字串尾的東西,表示字串的結尾!!
 
插入:
  1. int main()  
  2. {  
  3.    string s,  target = "book",   ins = "the ";  
  4.    getline(cin,s); //使用者輸入一字串  
  5.    int i,pos = 0;  
  6.    while((i=s.find(target,pos))!=string::npos) //find(要找book,從位置[pos]開始),直到結尾  
  7.    {  
  8.        s.insert(i,ins);  //插入的用法,從第i的位置插入ins字串  
  9.        pos = i + ins.size()+1; //pos是說,我找到第一個,之後還可能有再跑圈,避免重覆要把pos改掉  
  10.    }                                         
  11.    cout << s << endl;  
 
插前插後你就自己想想看啦~~
 
刪除:
  1. int main()  
  2. {  
  3.    string s,ins = "do";  
  4.    getline(cin,s);  
  5.    int i,pos = 0;  
  6.    while((i=s.find(ins,pos))!=string::npos)  
  7.    {  
  8.        s.erase(i,ins.size()); //刪除用法,從找到字的位置後幾個刪掉  
  9.        pos = i + ins.size()+1;  
  10.    }  
  11.    cout << s << endl;  
 
修改:
  1. int main()  
  2. {  
  3.    string s = string();   
  4.    s = "york ";  
  5.    cout << s << endl;  
  6.    s += "is a boy";  
  7.    cout << s << endl;  
  8.   
  9.    s.assign("life is beautiful",8,9); // 從位置8之後取9個字元  
  10.    cout << s <<endl;  
  11.    s.append(" islander",7); //取islander裡的前7個字,加到s字串後  
  12.    cout << s << endl;  
 
比對:
  1. int main()  
  2. {  
  3.    string s1 = "york";  
  4.    string s2 = "york";  
  5.    string s3 = "yorkk";  
  6.    cout << s1.compare(s2) << endl;       //s1和s2比較;s1=s2--->0  
  7.    cout << s1.compare(s3) << endl;       //s1<s3---->-1;出現負值都是右邊比左邊大  
  8.    cout << s1.compare(0,2,s1)<< endl;   //將york從位置0之後取2個子和s1比較;  
  9.    cout << s3.compare(0,4,s1)<< endl;  
  10. }
 
取子字串:
  1. int main()  
  2. {  
  3.    string s = "yorkisaboy";  
  4.    cout << s.substr(0,4) << " "//位置從0取到4  
  5.    cout << s.substr(4,2) << " ";  
  6.    cout << s.substr(6,1) << " ";  
  7.    cout << s.substr(7,3) << " ";  
  8. }  
 
搜尋  (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 << " 個數字字元";  
  • }
取代:
  1. int main()  
  2. {  
  3.    string s,s1,s2;  
  4.    cout << "請輸入一個字串:";  
  5.    getline(cin,s);  
  6.    cout << "請輸入要替換的字串:";  
  7.    getline(cin,s1);  
  8.    cout << "要將["<< s1 << "]換成?";  
  9.    getline(cin,s2);  
  10.    int i,pos = 0;  
  11.    unsigned int len1= s1.size(),len2 = s2.size();  
  12.    while((i=s.find(s1,pos))!=string::npos)  
  13.    {  
  14.        s.replace(i,len1,s2);   //從位置i到len1換成s2  
  15.        pos = i + len2 + 1;  
  16.    }  
  17.    cout << "新字串:"<< s;  
  18. }
取字元陣列在排序:
  1. int main()  
  2. {  
  3.    string s;  
  4.    cout << "請輸入一個字串:";  
  5.    cin >> s;  
  6.    int len = s.size();  
  7.    char *cstr = new char(len+1); //創一個位址給放字串  
  8.    strcpy(cstr,s.c_str());   //#include<cstring>一定要得標頭檔,c_str()就是你輸入的字串  
  9.    for(int i=0;i<len;i++)  
  10.    {  
  11.        for(int j=i+1;j<len;j++) //排序  
  12.        {  
  13.            if(cstr[i]>cstr[j])  
  14.            {  
  15.                char tmp = cstr[i];  
  16.                cstr[i] = cstr[j];  
  17.                cstr[j] = tmp;  
  18.            }  
  19.        }  
  20.    }  
  21.    cout << "將字串內容排序後:"<<cstr;  
  22.    delete [] cstr;  
  23. }
其實功能是可以混著用的,蠻多練習題目都是將他們活用,這樣就能省很多程式碼喔
 
END
arrow
arrow
    全站熱搜

    佑佑 發表在 痞客邦 留言(0) 人氣()