String容器的用法大全

String容器的用法大全

String容器的简介

本质:string是C++风格的字符串,而string本质上是一个类;

string和char * 区别:

① char * 是一个指针;

② string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器。

String容器的初始化

#include <iostream>  
using namespace std;  
#include <string>  
  
int main()  
{  
    const char ch[] = "6sjkasa";  
    string str(ch); // 用字符型数组进行初始化  
    string str1(str1); // 调用string容器的拷贝构造函数初始化  
    string str2("skajda"); // 调用普通构造函数进行初始化  
    string str4(4, 'a'); // 用字符常量初始化字符串的正确方法
} 

 

为什么调用拷贝构造函数?

当你传入的量是变量时,需要进行以下步骤:

 

字符串的赋值操作

#include <iostream>  
using namespace std;  
#include <string>  
  
int main()  
{  
    const char ch[] = "6sjkasa";  
    string str(ch); // 用字符型数组进行初始化  
    string str1(str); // 调用string容器的拷贝构造函数初始化  
    string str2("skajda"); // 调用普通构造函数进行初始化  
    string str3(4, 'a'); // 用字符常量初始化字符串的正确方法  
  
    string str4;  
    const char ch1[] = "saskjsk", ch2 = 'a';  
    str4 = ch1; // 用字符型数组赋值字符串  
    cout << str4 << endl;  
    str4 = ch2; // 用字符赋值字符串  
    cout << str4 << endl;  
    str4 = str2; // 用已有字符串赋值字符串  
    cout << str4 << endl;  
    str4.assign(str1); // 用assign成员函数进行字符串的赋值操作  
    cout << str4 << endl;  
    str4.assign(ch1,5); // 用assign成员函数进行字符串的赋值操作(将字符数组的前5个字符赋值给string对象)  
    cout << str4 << endl;  
    str4.assign(ch1); // 用assign成员函数进行字符串的赋值操作  
    cout << str4 << endl;  
    str4.assign(3,'f'); // 用assign成员函数进行字符串的赋值操作  
    cout << str4 << endl;  
}  

字符串的拼接

#include <iostream>  
using namespace std;  
#include <string>  
  
int main()  
{  
    string str1 = "shisishd";  
    string str2(1, 'd');  
    str2 += str1; // 在字符串尾部拼接字符串  
    cout << str2 << endl;  
    const char ch1[] = "666", ch2 = 'p';  
    str2 += ch1; // 字符串尾部拼接字符型数组  
    cout << str2 << endl;  
    str2 += ch2; // 字符串尾部拼接字符常量  
    cout << str2 << endl;   
    str2.append(ch1, 2); // 把字符型数组的前2个字符拼接到str2字符串的结尾  
    cout << str2 << endl;  
    str2.append(str1, 4, 3); // 将字符串str1的4-7字符拼接至str2字符串结尾  
    cout << str2 << endl;  
    str2.append(ch1); // 把字符型数组整个拼接至str2字符串的结尾  
    cout << str2 << endl;  
    str2.append(str1); // 把字符串str1整个拼接至str2字符串结尾  
    cout << str2 << endl;  
    str2.append(4, 'e'); // 把4个字符'e'拼接至str2字符串的结尾  
    cout << str2 << endl;  
}  

字符串的查找与替换

#include <iostream>  
#include <string>  
using namespace std;  
  
int main()  
{  
    string str("sjdjdshgjda"), str1("jd");  
    int pos = str.find(str1, 2);  
    cout << pos << endl;  // pos = 3
}  

这里我们看到有三个“jd”字符串,但是我们从第二个开始从左到右正先查找,因此“jd”第一次出现于第三个字符。

无论是Find函数还是rfind函数只要查找不到对应的字符都会返回-1表示没找到指定字符。

#include <iostream>  
#include <string>  
using namespace std;  
  
int main()  
{  
    string str("sjdjdshgjda"), str1("jd");  
    int pos = str.rfind(str1, 10);  
    cout << pos << endl;  // 8
}  

#include <iostream>  
#include <string>  
using namespace std;  
  
int main()  
{  
    string str("sjdjdshgjda"), str1("jd");  
    int pos = str.rfind(str1, 10);  
    cout << pos << endl;  
  
    str.replace(3, 2, "666"); // index=3-5的字符替换为"666"  
    pos = str.find("666", 0); // 从str第0个字符开始查找字符串"666"  
    str.replace(pos, sizeof("666"), "777");  
    cout << str << endl;  
}  

以上是find查找函数与replace替换函数相互搭配使用的案例。

 

以上的重载函数类型与程序示例中用法相似,这里就不再做重复介绍。

String容器的比较

#include <iostream>  
#include <string>  
using namespace std;  
  
int main()  
{  
    string str("sjahjdhja"), str1("654sajdhadhj");  
    int compare = str.compare(str1);  
    if (compare > 0)  
    {  
        cout << "str>str1" << endl;  
    }  
    else if(compare == 0)  
    {  
        cout << "str==str1" << endl;  
    }  
    else  
    {  
        cout << "str<str1" << endl;  
    }  
}  

当然,这个compare成员函数的第一个参数也可以换为“字符型数组”,compare函数原型如下:

 

String容器的访问

#include <iostream>  
using namespace std;  
#include <string>  
  
int main()  
{  
    string str = "shajhajhsjash";  
    cout << str[2] << endl;  // 只读
    str[2] = 'p';  // 写
    cout << str[2] << endl;  
    cout << str.at(4) << endl;  // 只读
    str.at(4) = 'o';  // 写
    cout << str.at(4) << endl;  
}  

我们看到我们可以像访问数组一样用[]来访问string中的char类型的元素,也可以用内置的at成员函数来访问string类中的char类型字符元素,同时我们也可以通过这个访问方法来读写特定字符。

String容器的插入与删除

#include <iostream>  
using namespace std;  
#include <string>  
  
int main()  
{  
    string str("666777888999"),str1("555");  
    str.insert(3, 3, 'a'); // 在3字符位置插入3个'a'字符  
    str.insert(0, str1); // 在0字符位置插入str1字符串  
    const char ch[] = "sjahja";  
    str.insert(0, ch, 2); // 在0字符位置插入ch字符型数组的前两个元素  
    cout << str << endl;  
  
    cout << str.find('6', 0) << endl;  
    cout << str.rfind('6', str.size()) << endl;  
    str.erase(str.find('6', 0), str.rfind('6', str.size()) - str.find('6', 0) + 1); // 字符串与find/rfind查找函数搭配使用  
    cout << str << endl;  
}  

注意:这里,我们一般使用find/rfind查找函数搭配插入/删除函数使用,一定要理解rfind你想查找函数的用法以及其返回值。

String字符串的子串的提取

#include <iostream>  
using namespace std;  
#include <string>  
  
int main()  
{  
    string str("666777888999"),str1("555");  
    str.insert(3, 3, 'a'); // 在3字符位置插入3个'a'字符  
    str.insert(0, str1); // 在0字符位置插入str1字符串  
    const char ch[] = "sjahja";  
    str.insert(0, ch, 2); // 在0字符位置插入ch字符型数组的前两个元素  
    cout << str << endl;  
  
    cout << str.find('6', 0) << endl;  
    cout << str.rfind('6', str.size()) << endl;  
    str.erase(str.find('6', 0), str.rfind('6', str.size()) - str.find('6', 0) + 1); // 字符串与find/rfind查找函数搭配使用  
    cout << str << endl;  
  
    string str2;  
    str2 = str.substr(str.find('9', 0), str.rfind('9', str.size()) - str.find('9', 0) + 1);   
    // 我们用提取字串的函数搭配find/rfind函数使用就可以提取出我们想要的一段字符  
    cout << str2 << endl;  
}  

我们一般提取子串时,都采用搭配find/rfind查找函数的方式来实现,这样我们可以提取我们想要的一段字符。

猜你喜欢

转载自blog.csdn.net/weixin_45590473/article/details/110452785
今日推荐