zoukankan      html  css  js  c++  java
  • 6.4 C++提取子字符串及字符串的比较

    参考:http://www.weixueyuan.net/view/6393.html

    总结:

      函数substr可以提取string字符串中的子字符串,该函数有两个参数,第一个参数为需要提取的子字符串的起始下标,第二个参数是需要提取的子字符串的长度。

      “==”、 “!=”、 “<=”、 “>=”、 “<”和“>”操作符都可以用于进行string类型字符串的比较,这些操作符两边都可以是string字符串,也可以一边是string字符串另一边是字符串数组。

    函数substr可以提取string字符串中的子字符串,该函数有两个参数,第一个参数为需要提取的子字符串的起始下标,第二个参数是需要提取的子字符串的长度。

    例1:

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string s1 = "first second third";
        string s2;
        s2 = s1.substr(6, 6);
        cout<< s1 <<endl;
        cout<< s2 <<endl;
        return 0;
    }

    程序运行结果:
    first second third
    second

    该函数同样会出现参数越界的情况,如果第一个参数越界则函数会抛出异常。在第一个参数没有越界的情况下,第二个参数仍然会导致越界,该函数的处理方式与前面提到的erase函数、replace函数相同,子字符串最多从第一个参数所指明的下标开始一直延续到字符串结尾。

    C++字符串的比较

    参考:http://www.weixueyuan.net/view/6395.html

    “==”、 “!=”、 “<=”、 “>=”、 “<”和“>”操作符都可以用于进行string类型字符串的比较,这些操作符两边都可以是string字符串,也可以一边是string字符串另一边是字符串数组。

    例1:

     
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string s1 = "secondsecondthird";
        string s2 = "secondthird";
        if( s1 == s2 )
            cout<< " == " <<endl;
        if( s1 != s2 )
            cout<< " != " <<endl;
        if( s1 < s2 )
            cout<< " < " <<endl;
        if( s1 > s2 )
            cout<< " > " <<endl;
        return 0;
    }

    程序最终运行结果:
    !=
    <

  • 相关阅读:
    JAVA-初步认识-第十一章-异常-原理异常对象的抛出throw
    shopex后台上传模板漏洞
    PHP使用1个crontab管理多个crontab任务
    HTML5跨浏览器表单及HTML5表单的渐进增强
    用Opera Mobile调试手机版网页【转】
    mootools里选择器$,$$,$E,$ES等的区别
    Call to undefined function bcscale()
    阿里云服务器数据库配置
    阿里云Mysql重置密码
    window.open窗口关闭后刷新父窗口代码
  • 原文地址:https://www.cnblogs.com/yongpan/p/7919527.html
Copyright © 2011-2022 走看看