首页 > 代码库 > 【模板】字符串小结

【模板】字符串小结

关于字符串的一些用法:

#include<iostream>
#include<cstring>
using namespace std;
int main(){
    /*
    char转string 
    char s1[3];
    cin>>s1;
    cout<<s1[2]<<endl;
    string s2 = s1;
    cout<<s2;
    */
    string ss = "ABC";
    char sa[100];
    strcpy(sa, ss.c_str());
    cout<<sa[2]<<endl;//string转char 
    
    string s1, s2, s3, s4;
    s1 = ss.substr(1, 1);
    s2 = ss.insert(1, "EF");
    s3 = ss.erase(2, 1);
    s4 = ss.replace(1, 1, "MN");
    cout<<s1<<" "<<s2<<" "<<s3<<" "<<s4<<endl;
    
    int p1, p2, p3;
    p1 = ss.find(C);
    p2 = ss.rfind(C); //最后一个关键字出现位置
    p3 = ss.find_first_of("AC");//任何一个首次在str1中出现的位置
    cout<<p1<<" "<<p2<<" "<<p3<<endl;
    
    return 0;
} 

 

【模板】字符串小结