zoukankan      html  css  js  c++  java
  • std::string::erase in C++ 之 erase()方法截取string的前几位、后几位

    Syntax 1: Erases all characters in a string 

    string& string ::erase ()
    // CPP code to illustrate
    // erase() function
     
    #include <iostream>
    #include <string>
    using namespace std;
     
    // Function to demo erase()
    void eraseDemo(string str)
    {
        // Deletes all characters
        str.erase();
     
        cout << "After erase() : ";
        cout << str;
    }
     
    // Driver code
    int main()
    {
        string str("Hello World!");
     
        cout << "Before erase() : ";
        cout << str << endl;
        eraseDemo(str);
     
        return 0;
    }
    cpp

    output:

    Before erase() : Hello World!
    After erase() : 

    Syntax 2: Erases all characters after position ‘pos’ 

    string& string ::erase (size_type pos)
    - Throw out_of_range if idx > size().
    // CPP code to illustrate working of
    // erase(idx)
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    // Function to demo erase
    void eraseDemo(string str)
    {
        // Deletes all characters except first one
        str.erase(1);
    
        cout << "After erase(idx) : ";
        cout << str;
    }
    
    // Driver code
    int main()
    {
        string str("Hello World!");
    
        cout << "Before erase(idx) : ";
        cout << str << endl;
        eraseDemo(str);
    
        return 0;
    }
    cpp

    output:

    Before erase(idx) : Hello World!
    After erase(idx) : H

    Syntax 3: Erases at most, len characters of *this, starting at index idx. 

    string& string ::erase (size_type idx, size_type len )
    - If len is missing, all remaining characters are removed.
    - Throw out_of_range if idx > size().
    - 
    // CPP code to illustrate
    // erase(size_type idx, size_type len )
    #include <iostream>
    #include <string>
    using namespace std;
    
    // Function to demo erase
    void eraseDemo(string str)
    {
        // Deletes 4 characters from index number 1
        str.erase(1, 4);
    
        cout << "After erase : ";
        cout << str;
    }
    
    // Driver code
    int main()
    {
        string str("Hello World!");
    
        cout << "Before erase : ";
        cout << str << endl;
        eraseDemo(str);
    
        return 0;
    }
    View Code

    output:

    Before erase : Hello World!
    After erase : H World!

    Syntax 4: Erase the single character at iterator position pos. 

    string& string ::erase (iterator pos)
    - Return the first character after the last character removed
    - If no such character is remaining then, returns 
      string::end() i.e. position after the last character.
    // CPP code to illustrate
    // erase(iterator pos)
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    // Function to demo erase
    void eraseDemo(string str)
    {
        // Deletes character at position 4
        str.erase(str.begin() + 4);
    
        cout << "After erase : ";
        cout << str;
    }
    
    // Driver code
    int main()
    {
        string str("Hello World!");
    
        cout << "Before erase : ";
        cout << str << endl;
        eraseDemo(str);
    
        return 0;
    }
    View Code

    output:

    Before erase : Hello World!
    After erase : Hell World!

    Syntax 5: Erase characters from iterator pos. to another iterator pos.

    string& string ::erase (iterator beg, iterator end )
    - Erases all characters of the range [ beg, end)
    - Returns end i.e. the first character after the
      last character removed.
    - If no such character is remaining then, returns 
      string::end() i.e. position after the last character
    // CPP code to illustrate
    // erase(iterator pos, iterator end)
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    // Function to demo erase
    void eraseDemo(string str)
    {
        // Deletes all characters between 0th index and
        // str.end() - 6
        str.erase(str.begin() + 0, str.end() - 6);
    
        cout << "After erase : ";
        cout << str;
    }
    
    // Driver code
    int main()
    {
        string str("Hello World!");
    
        cout << "Before erase : ";
        cout << str << endl;
        eraseDemo(str);
    
        return 0;
    }
    View Code

    output:

    Before erase : Hello World!
    After erase : World!

     


    from:https://www.geeksforgeeks.org/stdstringerase-in-cpp/

  • 相关阅读:
    php 爬虫采集
    深入浅出Java 23种设计模式,最全PDF版本终于开放下载了!!(文末有福利)
    【Nginx】如何格式化日志并推送到远程服务器?看完原来很简单!!
    【Nginx】如何为已安装的Nginx动态添加模块?看完我懂了!!
    【Nginx】如何配置Nginx日志?这是最全面的一篇了!!
    【Nginx】如何按日期分割Nginx日志?看这一篇就够了!!
    【Nginx】如何封禁IP和IP段?看完这篇我会了!!
    【Nginx】面试官竟然问我Nginx如何生成缩略图,还好我看了这篇文章!!
    【Nginx】实现负载均衡、限流、缓存、黑白名单和灰度发布,这是最全的一篇了!
    【Nginx】如何获取客户端真实IP、域名、协议、端口?看这一篇就够了!
  • 原文地址:https://www.cnblogs.com/PiaYie/p/14605287.html
Copyright © 2011-2022 走看看