算法数据结构和算法分析

C++中String处理常用方法总结

2018-11-24  本文已影响9人  ac_Jiaxu
字符串的题目大多是和模拟题挂钩,处理起来很麻烦,除非使用正则表达式,但是那玩意又很恶心。所以今天下午我就总结了一下C++里面关于字符串常用的方法,干货满满哦!
#include<iostream>
#include<string>
#include<sstream>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
int main(){
    cout<<"Commonly used string extraction methods\n";
    string s="xuhacker ni hao";
    string s1="h";
    string s3="zxcvbnm,./";
    unsigned long long i;
    //string s.find(s1,pos) return the location where s1 first appeared, and if pos does not exist, the default value of pos is zero
    if(s.find(s1)!=string::npos){
        i=s.find(s1);
        cout<<"the first position is: "<<i<<endl;
        i=s.find(s1,6);
        cout<<"the second postion is: "<<i<<endl;//return 12 if s1 does not exit ,retrun npos= 4294967295
    }
    //string s.erase
    //the first use of string.erase();
    s.erase(0,2);//return "hacker ni hao" -> Remove two characters from 0
    cout<<"the first use of erase is "<<s<<endl;
    //the second use of string.erase()
    s.erase(5);//return hacke 
    cout<<"the second use of erase is "<<s<<endl;
    //the third use of string.erase()
    string::iterator it;//the iterator of string
    it=s.begin()+2;
    s.erase(it);//the position is an iterator ,meanwhile,remove the element of this position return "hake"
    cout<<"the third use of erase is "<<s<<endl;
    //the fourth use of string erase();
    string::iterator first,last;
    first=s.begin()+1;
    last=s.end()-1;
    s.erase(first,last);//Remove the elements between first and second, including first and last
    cout<<"the fourth use of erase is "<<s<<endl;
    //the use of substr()
    //the first use of substr()
    string s4=s3.substr(6);//return "m,./"
    cout<<"the first use of substr is "<<s4<<endl; 
    string s5=s3.substr(6,2);//return "m,"
    cout<<"the second use of substr is "<<s5<<endl;
    string s6="asdf";
    string s7=s5.insert(1,s6);//insert s6 to the position of 1
    cout<<"s5 insert to s6 is "<<s7<<endl;
    //the second use of insert
    string::iterator it1;
    it1=s5.begin();
    s5.insert(it1,s7.begin()+1,s7.end()-1);
    cout<<"the second use of insert is "<<s5<<endl;
    //replace() which is in the algorithm
    string s_replace="1231131132/163.com";
    cout<<"old s_replace is: "<<s_replace<<endl;
    replace(s_replace.begin(),s_replace.end(),'/','@');
    cout<<"new s_replace is: "<<s_replace<<endl;
    //s_replace.replace(pos,len,string);
    s_replace.replace(3,5,"xu");
    cout<<"s_replace.replace() is: "<<s_replace<<endl;
    cout<<"=================sstream==================\n";
    //the use of istringstream :to separate each substring of an entire string read in with a space
    string s8="xuhacker . me";
    istringstream iss;
    iss.str(s8);
    //int len=s8.length();
    for(i=0;i<3;i++){
        string value;
        iss>>value;
        cout<<"print this value: "<<value<<"\n";
    }
    cout<<"print the str in iss "<<iss.str()<<"\n";
    //the use of ostringstream :
    ostringstream oss;
    oss<<"the use of oss is "<<s8<<endl;
    cout<<oss.str();
    //if you want to use oss again, you should clear it.
    oss.clear();
    //string s9=" ";
    oss.str("");
    cout<<"test it again (there is nothing :)"<<oss.str()<<endl;
    //the use of stringstream:int to string
    stringstream ss;
    string s9;
    int val=77;
    ss<<val;//puts val  
    ss>>s9;//transform
    cout<<"transform s9: "<<s9<<endl;
    //the use of ss: string to int (twice),need use clear() there;
    int a=0,b=0;
    ss.clear();//"ss<<val" has used
    ss<<"123";//puts the string 
    ss>> a;
    cout<<"a is: "<<a<<endl;
    // because "ss>>a" has used
    ss.clear();
    ss<<"234";
    ss>>b;
    cout<<"b is: "<<b<<endl;
    cout<<"=============cstring==============\n";
    char s11[100];
    string s10;
    getchar();
    getline(cin,s10);//read a line  same as "get(s11)"
    //strcmp()
    if(s10=="xu"/*strcmp(s11,"xu")*/)
        cout<<"The two equal!!!\n";
    else
        cout<<"they are not equal!!!\n";
    //strcpy()
    s10="Yo.";/*The assignment strcpy(s11,"Yo.")*/
        cout<<"S10 assigned values! s10 is: "<<s10<<"\n";
    strcpy(s11,"Yo.");
        cout<<"S11 assigned values! s11 is: "<<s11<<"\n";
    //strcat()
    s10+="Come on,xu!";
        cout<<"s10 has connected with a new string,it is: "<<s10<<endl;
    strcat(s11,"Come on,xu!") ;
        cout<<"s11 has connected with a new string,it is: "<<s11<<endl;
    //strtok();
    char s12[]="xuhacker-me-hello-world";
    char *t=strtok(s12,"-");//Cut the parent string into substrings
    cout<<"After cutting the string: ";
    while(t){
        cout<<t<<" ";
        t=strtok(NULL,"-");
    }
    cout<<endl;
    //sscanf、sprintf same as sstream 
    cout<<"====================cctype========================"<<endl;
    //isalpha : isdigit、 isupper 、islower、istolower、(to small)、istoupper(to big) 
     string s13="123alpha232./.";
     for(int i=0;i<s13.length();i++){
        if(isalpha(s13[i])){
            cout<<s13[i]<<" ";
         }
     }
     cout<<endl;
     cout<<"all those funs are used to judge a string or a char\n";
    return 0;
}

运行结果:

TIM截图20181124182930_test.png
上一篇下一篇

猜你喜欢

热点阅读