zoukankan      html  css  js  c++  java
  • string的常见用法

    string的使用

    c语言中一般使用字符数组来存放字符串,但是这样使用起来容易出现一些错误。为了避免使用时候的麻烦和错误,c++在stl中加string类,对常用的功能进行了封装。

    string的定义

    1.头文件包含string

    # include <string>
    # include <iostream>
    using namespace std;
    

    2.定义和初始化

    string str;
    string str="abcd";
    

    string内容访问

    一般来说可以通过字符串数组方式访问string

    通过下标访问

    # include <iostream>
    using namespace std;
    int main(void)
    {
        string str="abcd";
        for(int i=0;i<str.length();i++)
        {
            cout<<str[i]<<' '<<endl;
        }
        return 0;
    }
    

    输入输出整个字符串

    # include <iostream>
    using namespace std;
    int main(void)
    {
        string str;
        cin>>str;
        cout<<str<<endl;
        return 0;
    }
    

    通过迭代器访问

    # include <iostream>
    using namespace std;
    int main(void)
    {
        string str="abcde";
        for(string::iterator it=str.begin();it!=str.end();it++)
        {
            cout<<*it<<endl;
        }
        return 0;
    }
    

    string常见函数

    字符串拼接

    # include <iostream>
    using namespace std;
    int main(void)
    {
       string str1="abc";
       string str2="def";
       string str3;
       str3=str1+str2;
       str2+=str1;
       cout<<str1<<' '<<str2<<' '<<str3<<endl;
       return 0;
    }
    

    字符串比较

    按照字典的顺序来比较字符串

    # include <iostream>
    using namespace std;
    int main(void)
    {
       string str1="abc";
       string str2="def";
       if(str1>str2)
       cout<<"str1>str2"<<endl;
       else
       cout<<"str1<str2"<<endl;
       return 0;
    }
    

    获取字符串长度

    size()和length()函数都是获取当前字符串的长度,区别不大

    # include <iostream>
    using namespace std;
    int main(void)
    {
       string str1="abc";
       string str2="def";
       cout<<str1.length()<<' '<<str2.size()<<endl;
       return 0;
    }
    

    插入字符串

    insert(pos,string)

    # include <iostream>
    using namespace std;
    int main(void)
    {
       string str1="Hello";
       string str2="world";
       str1.insert(2,str2);
       cout<<str1<<endl;
       //outcome=Heworldllo
       return 0;
    }
    

    insert(it,it2,it3)

    it是要插入的位置
    it2,it3是要插入字符的首尾迭代器

    # include <iostream>
    using namespace std;
    int main(void)
    {
       string str1="Hello";
       string str2="world";
       str.insert(str1.begin()+2,str2.begin(),str2.end());
       //outcome=Heworldllo
       return 0;
    }
    

    删除单个元素或者区间元素

    删除单个元素

    # include <iostream>
    using namespace std;
    int main(void)
    {
       string str="abcdef";
       str.erase(str.begin()+2);//删除第三个元素
       cout<<str<<endl;//abdef
       return 0;
    }
    

    删除区间元素

    erase(first,lase),first为要删除区间的首迭代器,end是要删除区间的尾部迭代器

    # include <iostream>
    using namespace std;
    int main(void)
    {
       string str="abcdef";
       str.erase(str.begin(),str.begin()+2);
       cout<<str<<endl;//[str.begin(),str.begin()+2)区间的元素
       return 0;
    }
    

    erase(pos,length),pos删除开始的位置,而lenth是要删除的长度

    # include <iostream>
    using namespace std;
    int main(void)
    {
       string str="abcdef";
       str.erase(2,2);
       cout<<str<<endl;//"abef"
       return 0;
    }
    

    删除所有数据

    # include <iostream>
    using namespace std;
    int main(void)
    {
       string str="abcdef";
       str.clear();
       cout<<str.length()<<endl;//0
       return 0;
    }
    

    字符串切片substr

    # include <iostream>
    using namespace std;
    int main(void)
    {
       string str="abcdef";
       str.substr(0,4);//[0,4)
       cout<<str<<endl;//abcd
       return 0;
    }
    

    string::npos

    string::npos是一个常数,其本身值为-1,但是由于是unsigned_int类型,因此实际上也可以认为是unsigned_int的最大值,string::npos是find函数失配的返回值

    字符串匹配find

    str.find(str2),str2是str的子字符串,返回str第一次出现str2的位置
    str.find(str2,pos),str从pos位开始匹配str2,返回第一次出现位置的下标

    # include <iostream>
    using namespace std;
    int main(void)
    {
       string str="Thank you for your smile";
       string str2="you";
       string str3="me";
       if(str.find(str2)!=str::npos)
       cout<<str.find(str2);
       if(str.find(str2,7)!=str::npos)
       cout<<str.find(str2,7);
       if(str.find(str3)!=str::npos)
       cout<<str.find(str3);
       return 0;
    }
    

    字符串替换

    str.replace(pos,len,str2),str从pos位置起,长度为len的范围,全部替换为str2
    str.replace(it1,it2,str2),str迭代器[it1,it2)范围替换为str2

    # include <iostream>
    using namespace std;
    int main(void)
    {
       string str1="Hello World! I am coming!";
       string str2="me";
       string str3="you";
       str1.replace(0,2,str2);
       str1.replace(str1.begin(),str1.end(),str3);
       cout<<str1<<endl;
       str1.replace(0)
       return 0;
    }
    
  • 相关阅读:
    TCP/IP协议栈概述及各层包头分析
    Maven:Non-resolvable parent POM: Failure to find错误
    mysql alter 用法,修改表,字段等信息
    PowerDesigner16 设置导出sql文件的编码
    linux iptables开放/关闭端口命令
    Enterprise Architect 13 : 需求建模 自动命名并计数
    Enterprise Architect 13 : 将绘制的图形导出成图片 或者 拷贝到剪贴板中
    Enterprise Architect 13 : 设置默认代码环境
    使用MyBatis查询 返回类型为int,但是当查询结果为空NULL,报异常的解决方法
    PowerDesigner16 修改表或表的字段Name的时候不让Code不自动跟着变
  • 原文地址:https://www.cnblogs.com/mengxiaoleng/p/11371470.html
Copyright © 2011-2022 走看看