String function uses notes

The following is a study of learning string notes for reference only
most of the content from the C language learning network, I just explained to my understanding,And behind the whole lazy

structure

string s1();  // si = ""
string s2("Hello");  // s2 = "Hello"
string s3(4, 'K');  // s3 = "KKKK"
string s4("12345", 1, 3);  //s4 = "234",即 "12345" 的从下标 1 开始,长度为 3 的子串

Seek length

s1.size();
s1.length();

There was no distinction of any kind
not to be confused with strlen ()!

Connection String

There are two approaches

1.s.append

string s1("123"),s2("abc")
s1.append(s2);//s1=123abc;
s1.append(s2,1,2);//相当于在s1后面添加从s2中1到2的字符
s1.append(3,'K')//在s1后面添加3个K
s1.append("ABCDE",2,3)//添加"ABCDE"的位置2到3的字串

2.+

string s1("123"),s2("abc")
s1=s1+s2;//s1="123abc"

String comparison

Also there are two ways

1.<,<=,==,!=,>=,>

This does not explain

2.s.compare()

string s1("hello"), s2("hello, world");
int n = s1.compare(s2);
n = s1.compare(1, 2, s2, 0, 3);  //比较s1的子串 (1,2) 和s2的子串 (0,3)
n = s1.compare(0, 2, s2);  // 比较s1的子串 (0,2) 和 s2
n = s1.compare("Hello");
n = s1.compare(1, 2, "Hello");  //比较 s1 的子串(1,2)和"Hello”
n = s1.compare(1, 2, "Hello", 1, 2);  //比较 s1 在位置1到2的字串和 "Hello" 在位置1至2的子串

Praying string

string s1 = "this is ok";
string s2 = s1.substr(2, 4);  // s2 = "is i"
s2 = s1.substr(2);  // s2 = "is is ok"

Exchange two strings

s1.swap(s2);

Find and character substring

Some lookup string class and character substring member function returns the value of the position they are in the sub-strings or character string objects string (i.e., subscript). If you can not find out, string :: npos is returned. string:: npos string class is defined in a static constant. These functions are as follows:

  • find: Find position of the substring from front to back or characters that appear.
  • rfind: from the back to find the position of a substring or character appears.
  • find_first_of: front to back to find another string of characters included where appearance. For example: s1.find_first_of ( "abc"); // Find any position s1 "abc" a character first appears once
    find_last_of: forward lookup from another character string included where appear.
  • find_first_not_of: front to back to find another string of characters not contained where appearance.
  • find_last_not_of: from the back to find another string of characters not contained where appearance.

The following is a sample program to find a member function string class.

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s1("Source Code");
    int n;
    if ((n = s1.find('u')) != string::npos) //查找 u 出现的位置
        cout << "1) " << n << "," << s1.substr(n) << endl;
    //输出 l)2,urce Code
    if ((n = s1.find("Source", 3)) == string::npos)
        //从下标3开始查找"Source",找不到
        cout << "2) " << "Not Found" << endl;  //输出 2) Not Found
    if ((n = s1.find("Co")) != string::npos)
        //查找子串"Co"。能找到,返回"Co"的位置
        cout << "3) " << n << ", " << s1.substr(n) << endl;
    //输出 3) 7, Code
    if ((n = s1.find_first_of("ceo")) != string::npos)
        //查找第一次出现或 'c'、'e'或'o'的位置
        cout << "4) " << n << ", " << s1.substr(n) << endl;
    //输出 4) l, ource Code
    if ((n = s1.find_last_of('e')) != string::npos)
        //查找最后一个 'e' 的位置
        cout << "5) " << n << ", " << s1.substr(n) << endl;  //输出 5) 10, e
    if ((n = s1.find_first_not_of("eou", 1)) != string::npos)
        //从下标1开始查找第一次出现非 'e'、'o' 或 'u' 字符的位置
        cout << "6) " << n << ", " << s1.substr(n) << endl;
    //输出 6) 3, rce Code
    return 0;
}

Delete substring

string s1("Real Steel");
s1.erase(1, 3);  //删除子串(1, 3),此后 s1 = "R Steel"
s1.erase(5);  //删除位置在5及其后面的所有字符,此后 s1 = "R Ste"

Guess you like

Origin www.cnblogs.com/Two-Feb/p/11708728.html