关于string 的简单应用

声明||作用

string类本不是STL的容器,但是它与STL容器有着很多相似的操作,因此,把string放在这里一起进行介绍。 
之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下的需要。我们尽可以把它看成是C++的基本数据类型。 
首先,为了在我们的程序中使用string类型,我们必须包含头文件。如下:

#include  <string>  // 注意这里不是string.h,string.h是C字符串头文件

1.定义:

string s1;      默认构造函数,s1为空串
string s2(s1);   将s2初始化为s1的一个副本
string s3("valuee");   将s3初始化一个字符串面值副本
string s4(n,'c');   将s4 初始化为字符'c'的n个副本,即重复n次‘c'
cin>>s5;  读取有效字符到遇到空格
getline(cin,s6);  读取字符到遇到换行,空格可读入,知道‘
’结束(练习在下一个代码中),
getline(cin,s7,'a'); 一个直到‘a’结束,其中任何字符包括'
'都能够读入

 附上代码以便学习

#include <iostream>  
#include <string>  
using namespace std;  
int main()  
{  
    string s1;  
    s1="i love you";  
    string s2(s1);  //把s2初始化为s1的一个副本,注意写法,不能前面先定义s2的类型后面直接写,也不能定义两次s2  
    string s3("value");  //将s3初始化一个字符串面值副本  
    string s4(10,'s');   //将s4初始化为字符‘s'的10个副本  
    /*注意字符串面值与标准库string不是同一个类型*/  
    cout<<s2<<" "<<s3<<" "<<s4<<endl;  
    string s5;  
    while(cin>>s5)  //这里可以输入“  hello world  ”测试,发现只读取有效字符到遇到空格结束  
    {  
        cout<<s5<<endl;  
    }  
    return 0;  
}  
View Code

 2.简单应用:

1.赋值操作符(=)

// 定义空字符串str1
string str1;
// 定义字符串str2
string str2("HelloWorld");
// 将字符串str2赋值给str1
str1 = str2

 2.左移操作符(<<)

// 定义字符串str
string str("HelloWorld");
// 输出字符串
cout << str <<endl;

3.右移操作符(>>)

// 定义空字符串
string str;
// 标准输入到字符串str
cin >> str;
// 输出字符串
cout << str << endl;

4.数组下标操作符([])

// 定义字符串str
string str("HelloWorld");
// 获取第5个字符
char c5 = str[5];
cout << c5 << endl;
// 将第5个字符改为'A'
str[5] = 'A';
cout << str << endl;

5.加法运算符

// 定义空字符串str1
string str1;
// 定义字符串str2
string str2("Hello");
// 将字符串str2和"World"相加并赋值给str1
str1 = str2 + "World";
// 输出str1
cout << str1 << endl;
输出 哈喽我

6.获取字符串的长度

// 定义字符串str
string str("HelloWorld");
// 获取字符串的长度
int len = str.length();
// 输出字符串长度
cout << "len = " << len << endl;

7.判断字符串是否为空 

// 定义字符串str
string str("HelloWorld");
// 判断字符串是否为空
bool isEmpty = str.empty();
// 输出
cout << "isEmpty = " << isEmpty << endl;

8.字符串的比较

// 定义字符串str1
string str1("HelloWorld");
// 定义字符串str2
string str2("HelloWorld");
// 比较两个字符串:相等(0),str1>str2(1),str1<str2(-1)
bool result = str1.compare(str2);
// 输出
cout << "result = " << result << endl;

9.字符串的子串

// 定义字符串str
string str("HelloWorld");
// 截取从2位置开始的字符,截取5个
str = str.substr(2, 5);
// 输出str
cout << "substr = " << str << endl;

10.字符串的查找

string str("HelloWorld");
/*
字符串的正向查找
    find(char ch,int pos):返回字符ch的实际位置,从pos位置开始往后查找,没有返回-1
    find(char * str,int pos):返回字符串str的首字符位置,从pos位置开始往后查找,没有返回-1
*/
int pos1 = str.find('W', 3);
cout << "字符'W'出现的位置:" << pos1 << endl;
int pos2 = str.find("World", 3);
cout << "字符串"World"出现的位置:" << pos2 << endl;
/*
字符串的反向查找
    rfind(char ch,int pos):返回字符ch的实际位置,从pos位置开始往前查找,没有返回-1
    rfind(char * str,int pos):返回字符串str的首字符位置,从pos位置开始往前查找,没有返回-1
*/
int pos3 = str.rfind('o', 5);
cout << "字符'o'出现的位置:" << pos3 << endl;
int pos4 = str.rfind("World", 3);
cout << "字符"World"出现的位置:" << pos4 << endl;

11.字符串的替换

// 定义字符串str
string str("HelloWorld");
// 替换:删除从第2个字符串开始后面的5个字符,然后再插入字符串"Test"
str.replace(2, 5, "Test");
// 输出
cout << str << endl;

12.字符串的删除

// 定义字符串str
string str("HelloWorld");
// 删除字符串的从第2个位置开始的5个字符
str = str.erase(2, 5);
// 输出
cout << str << endl;

13.字符串的插入

// 定义字符串str
string str("HelloWorld");
// 在第2个位置插入字符串"Test"
str.insert(2, "Test");
// 在5个位置插入10个字符'A'
str.insert(5, 10, 'A');
// 输出
cout << str << endl;

14

// 定义字空字符串str1
string str1;
// 定义字符串str2
string str2("HelloWorld");
// 将字符串str2的从第5个位置后的5个字符赋值给str1
str1.assign(str2, 5, 5);
// 输出
cout << str1 << endl;

.字符串的赋值

原文地址:https://www.cnblogs.com/shuaihui520/p/8973125.html