七、字符串string

字符串string

一、简介

string 类 是一个模板类, 它的定义如下:

typedef basic_string<char> string;

二、使用

2.1 初始化

void init()
{
    string s1("steven");
    string s2 = "alice";
    string s3(8, 'x');
    string s4;
    s4 = 'h';

    cout<<s1<<endl<<s2<<endl<<s3<<endl<<s4<<endl;
}

输出:

steven
alice
xxxxxxxx
h

2.2 赋值、连接

void assignAndAppend()
{
    //赋值 
    string s1("hello, world"), s2, s3, s4;
    s2 = s1;
    s3.assign(s1);
    s4.assign(s1, 0, 5); //下标0开始复制5个字符给s4


    for(int i=0; i<s1.length();i++)
        cout<<s1.at(i);
    cout<<endl;

    //连接 
    string s6 = "hahaha";
    s6 += s1;
    string s7 = "hehe";
    s7.append(s1);
    string s8 = "heihei";
    s8.append(s1, 0, 5); //追加s1下标1开始的5个字符给s8

    cout<<s2<<endl<<s3<<endl<<s4<<endl;
    cout<<s6<<endl<<s7<<endl<<s8<<endl;
} 

输出:

hello, world
hello, world
hello, world
hello
hahahahello, world
hehehello, world
heiheihello

2.3 比较

void comp()
{
    string s1 = "hello";
    string s2 = "hello";
    string s3 = "hell";
    string s4 = "helll";
    string s5 = "hellll";

    bool b1 = (s1==s2);
    bool b2 = (s1>s3);
    bool b3 = (s1>s4);
    bool b4 = (s1>s5);

    int f1 = s1.compare(s2);
    int f2 = s1.compare(s3);
    int f3 = s1.compare(s4);
    int f4 = s1.compare(s5);

    cout<<b1<<endl<<b2<<endl<<b3<<endl<<b4<<endl;
    cout<<f1<<endl<<f2<<endl<<f3<<endl<<f4<<endl;
}

输出:

1
1
1
1
0
1
1
1

2.4 特性

void feature()
{
    string s1 = "hello, world";

    cout<<s1.capacity()<<endl;
    cout<<s1.max_size()<<endl;
    cout<<s1.length()<<endl;
    cout<<s1.size()<<endl;
    cout<<s1.empty()<<endl;

    s1.resize(s1.length() + 10);


    cout<<s1.capacity()<<endl;
    cout<<s1.max_size()<<endl;
    cout<<s1.length()<<endl;
    cout<<s1.size()<<endl;
}

输出:

12
4611686018427387897
12
12
0
24
4611686018427387897
22
22

2.5 查找

void find()
{
    string s1 = "hello, worlod";

    cout<<s1.find("lo")<<endl;
    cout<<s1.find("abc")<<endl;
    cout<<s1.rfind("lo")<<endl;
    cout<<s1.rfind("abc")<<endl;
    cout<<s1.find_first_of("abcde")<<endl;  //只要abcde有一个字母存在s1中即可 
    cout<<s1.find_first_of("abc")<<endl;
    cout<<s1.find_last_of("abcde")<<endl;
    cout<<s1.find_last_of("abc")<<endl;
    cout<<s1.find_first_not_of("abcde")<<endl;
    cout<<s1.find_first_not_of("hello, world")<<endl;
    cout<<s1.find_last_not_of("abcde")<<endl;
    cout<<s1.find_last_not_of("hello, world")<<endl;

    cout<<s1.find("lo", 1)<<endl; //从下标1开始寻找s1中的"lo"
    cout<<s1.find("lo", 5)<<endl;
    cout<<s1.find("lo", 12)<<endl;
}

输出:

3
18446744073709551615
10
18446744073709551615
1
18446744073709551615
12
18446744073709551615
0
18446744073709551615
11
18446744073709551615
3
10
18446744073709551615

2.6 替换

void replace()
{
    string s1 = "hello, world";
    s1.replace(2, 3, "haha"); //将s1中下标2开始的3个字符替换为"haha"
    cout<<s1<<endl;


    string s2 = "hello, world";
    s2.replace(2, 3, "haha", 1, 2); //将s1中下标2开始的3个字符替换为字符串"haha"中下标1开始的2个字符
    cout<<s2<<endl;
}

输出:

hehaha, world
heah, world

2.7 插入

void insert()
{
    string s1 = "hello, world";
    string s2 = "insert";
    s1.insert(5, s2); //将s2插入到s1中下标5的位置
    cout<<s1<<endl;

    s1.insert(2, s2, 5, 3); //将s2下标5开始的3个字符插入到s1中下标2的位置
    cout<<s1<<endl;
}

输出:

helloinsert, world
hetlloinsert, world

2.8 转换为char *

void transform()
{
    string s1("hello, world");
    cout<<s1.c_str()<<endl;

    const char * p1 = s1.data();
    for(int i=0;i<s1.length();i++)
        printf("%c", *(p1+i));
    cout<<endl;

    char *p2 = new char[s1.length()+1];
    s1.copy(p2, 5, 0);
    p2[5]=0;
    cout<<p2<<endl;
}

输出:

hello, world
hello, world
hello

2.9 其他

void other()
{
    //子串 
    string s1 = "hello, world", s2;
    s2 = s1.substr(4, 5);
    cout<<s2<<endl;

    //擦除 
    s1.erase(5);
    cout<<s1<<endl;
    cout<<s1.length()<<endl;

    //交换 
    string s3 = "aaaaa", s4 = "hhhhh";
    s3.swap(s4);
    cout<<s3<<endl<<s4<<endl;
}

输出:

o, wo
hello
5
hhhhh
aaaaa

你可能感兴趣的:(C++,String,字符串)