C++学习日记(字符串)

c语言中处理字符串使用的是字符数组 char[]
c++中提供了string类,在做字符串操作时需要引用string头文件

string对象的定义和初始化

//字符串的建立和初始化
#include<iostream>
#include<string>
using namespace std;
void main()
{
    char *mystr = "hello,world";
    string s1;   //建立一个空字符串
    string s2("welcome"); //建立字符串并初始化为welcome
    string s3(mystr, 0, 5); //从mystr的0-5位置取出复制给S3
    string s4(4,'c'); //将s4初始化为4个c
    string s5(mystr, 6); //将mystr中的前6个字符赋值给s5
    string s6(s2);  //复制s2到s6中
    cout << s1 << endl;
    cout << s2 << endl;
    cout << s3 << endl;
    cout << s4 << endl;
    cout << s5 << endl;
    cout << s6 << endl;
    cin.get();
}

字符串类的成员函数

unsigned legth() :返回本字符串对象长度

unsigned size():返回字符串对象的大小

string &append(const char *s):将字符串s添加到源字符串的末尾

string &append(const string *s,unsigned n):将字符串s的前n个字符添加到原串的末尾

string &append(const string &str,unsigned pos,unsigned n):将字符串对str从pos位置开始后的n个字符加到原字符串后面

int compare(const string &str):将原串与str进行比较,原串小于str则返回-1,相等返回0,大于str返回1

string insert(unsigned p0,const string &str,unsigned pos,unsigned n):将字符串str从pos位置开始的n个字符插入到源字符串的p0处

string substr(unsigned pos,unsigned n):返回从pos位置开始的n个字符构成的字符串对象

unsigned find(string &str,unsigned pos=0):在原串中查找字符串str,返回第一次出现的位置,没有找到则返回string::npos

string &replace(unsigned p0,unsigned n0,string &str):用str替换原串中从p0开始到n0结束的字符串

void swap(string str):交换两个字符串
#include<iostream>
#include<string>
using namespace std;
void main()
{
    //定义一个长串
    string s1("this is a string test");
    //定义新串
    string s2;
    //定义一个查找串
    string s3("str");
    //存储字符串在原串中的位置
    int pos;
    cout << "length()方法使用的演示,s1串的长度为:" << s1.length() << endl;
    cout << "size()方法使用的演示,s1串的大小为:" << s1.size() << endl;
    cout << "append()方法演示:" << endl;
    cout << "在原串后附加字母:";
    s1.append("  haha");
    cout << s1 << endl;
    cout << "向特定的位置添加字符:";
    s1.append("haha", 10);
    cout << s1 << endl;
    cout << "assign()方法使用演示:";
    s1.assign("hello,word", 0, 10);
    cout << s1 << endl;
    cout << "insert()方法使用演示:";
    s1.insert(2, "good", 0, 4);
    cout << s1 << endl;
    cout << "substr()方法使用演示:";
    s2 = s1.substr(0, 2);
    cout << s2 << endl;
    cout << "find()使用方法演示:";
    pos = s1.find("good");
    if (pos != -1)
    {
        cout << pos << endl;
    }
    else
    {
        cout << "未找到字符串" << endl;
    }
    cout << "replace()方法使用演示:";
    s1.replace(1, 2, "we");
    cout << s1 << endl;
    cout << "compare()演示:" << s1.compare(s3) << endl;
    cout << "swap()演示:";
    s1.swap(s3);
    cout << s1 << endl;
    string s4("ssss");
    string s5("www");
    cout << s4 << endl;
    cout << s5 << endl;
    cout << s4.compare(s5) << endl;
    system("pause");
}

这里写图片描述

字符串处理类的操作符

s1+s2:连接两个字符串
s3=s1:将s1赋值给s3
+=:与另一个字符串连接后赋值给本身
关系运算符:> < <=…
[i]存取字符串中第i个元素的值
<<输出一个字符串
/ >>输入一个字符串

#include<iostream>
#include<string>
using namespace std;
void main()
{
    string s1,s2,s3;
    cout << "请输入字符串s1:";
    cin >> s1;
    cout << endl;
    cout << "请输入字符串s2:";
    cin >> s2;
    cout << endl;
    cout << "s3=s1+s2,s3= " << s1 + s2 << endl;
    s3 = s1;
    cout << "s3=s1,s3= " << s3 << endl;
    s3 += s1;
    cout << "s3+=s1,s3=" << s3 << endl;
    if (s1 > s3)
    {
        cout << "s1>s3" << endl;
    }
    else if (s1 < s3)
    {
        cout << "s1<s3" << endl;
    }
    else
    {
        cout << "s1=s3" << endl;
    }
    cout << "s2[2]= " << s2[2] << endl;
    system("pause");
}

这里写图片描述

字符串类中的指针

begin():返回第一个字符的位置
end():返回最后一个字符的位置
rbegin():反向返回字符串对象第一个字符的位置
rend():反向返回字符串对象最后一个字符的位置

#include<iostream>
#include<string>
using namespace std;
void main()
{
    string s1 = "hello,world";
    string::iterator ps1;
    const string s2 = "welcome";
    string::const_iterator ps2;
    string::reverse_iterator rptr1;
    string::const_reverse_iterator rptr2;
    cout << "普通字符串指针使用演示:" << endl;
    ps1 = s1.begin();
    for (; ps1 < s1.end(); ps1++)
    {
        cout << *ps1;
    }
    cout << endl;
    cout << "普通反向字符串指针使用演示:" << endl;
    rptr1 = s1.rbegin();
    for (; rptr1 < s1.rend(); rptr1++)
    {
        cout << *rptr1;
    }
    cout << endl;
    cout << "常字符串指针使用演示:" << endl;
    ps2 = s2.begin();
    for (; ps2 < s2.end(); ps2++)
    {
        cout << *ps2;
    }
    cout << endl;
    cout << "普通反向字符串指针使用演示:" << endl;
    rptr2 = s2.rbegin();
    for (; rptr2 < s2.rend(); rptr2++)
    {
        cout << *rptr2;
    }
    cout << endl;
    system("pause");
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/michaelzzk/article/details/80402766