String 字符串操作容器是C++标准中实现的重要容器,其主要用于对字符串的高效处理,它和C风格中的string.h并不是同一个库,两个库有极大的差距,C库中的string.h主要面向过程提供一些处理函数,而C++库中的string则是基于类实现的更高效的一种字符串处理方法集,类中提供了非常方便的成员函数供我们使用.
如下一段C++代码,展示了如何使用STL字符串的不同构造函数对字符串进行赋值和初始化。
在代码中,首先定义了字符串变量str,并将其初始化为"hello lyshark"。然后,使用构造函数将字符串str中的内容全部复制到新的字符串变量str_1中。接着,使用构造函数从字符串str的第2个元素开始,复制5个元素,并赋值给新的字符串变量str_2。
使用构造函数复制字符串str中的所有元素,并赋值给新的字符串变量str_3。接下来,将字符数组ch中的前3个元素赋值给新的字符串变量str_4。最后,使用构造函数将5个字符x组成的字符串xxxxx赋值给新的字符串变量str_5。
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string str("hello lyshark"); // 定义一个字符串
string str_1(str); // 构造函数,将 str中的内容全部复制到str_1
string str_2(str, 2, 5); // 构造函数,从字符串str的第2个元素开始,复制5个元素,赋值给str_2
string str_3(str.begin(), str.end()); // 复制字符串 str 的所有元素,并赋值给 str_3
char ch[] = "lyshark";
string str_4(ch, 3); // 将字符串ch的前5个元素赋值给str_4
string str_5(5, 'x'); // 将 5 个 'X' 组成的字符串 "XXXXX" 赋值给 str_5
system("pause");
return 0;
}如下C++代码,展示了如何使用STL字符串中的assign()函数对字符串进行赋值和操作。
在代码中,首先定义了字符串变量str和new_str,并将其初始化为"lyshark"。然后,使用基本的对象赋值将字符串str的内容赋值给new_str,即new_str = str。
接着,定义了三个新的字符串变量s1、s2和s3,并使用assign()函数对其进行初始化。使用assign()函数的第一个形式,将字符串str从第1位开始向后截取4个字符,并赋值给字符串s1。使用assign()函数的第二个形式,将5个字符A填充到字符串s2中。使用assign()函数的第三个形式,未指定任何参数,因此字符串s3被初始化为空字符串。
最后,使用cout输出字符串s3的内容。由于字符串s3的内容为空,因此输出结果为空行。
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string str,new_str;
str = "lyshark"; // 基本的对象赋值
new_str.assign(str); // 把str中的数据赋值给new_str
string s1, s2, s3;
s1.assign(str, 1, 4); // 字符串截取从str中第1位开始向后截取4个字符
s2.assign(5, 'A'); // 想字符串中填充5个A
cout << s3 << endl;
system("pause");
return 0;
}如下C++代码,展示了如何使用STL字符串中的[]运算符和at()函数遍历字符串,并介绍了在遍历时如何避免越界访问字符串。
在代码中,首先定义了字符串变量str,并将其初始化为"hello lyshark"。
使用[]运算符遍历字符串str中的所有字符,并输出每个字符。需要注意的是,使用[]运算符访问字符串时不能保证索引的有效性,如果访问越界,程序会直接崩溃。
使用at()函数遍历字符串str中的所有字符,并输出每个字符。使用at()函数访问字符串时,如果索引越界,则会抛出out_of_range异常。为了避免程序崩溃,使用try…catch语句来捕获异常并进行处理。
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
int main(int argc, char* argv[])
{
string str = "hello lyshark";
// 第一种遍历如果字符串越界,会直接挂掉
for (int x = 0; x < str.size(); x++)
cout << str[x] << endl;
// 第二种at遍历,如果越界会抛出异常
try
{
for (int x = 0; x < str.size(); x++)
cout << str[x] << endl;
}
catch (out_of_range &e)
{
cout << "异常类型: " << e.what() << endl;
}
system("pause");
return 0;
}如下C++代码,展示了如何使用STL字符串中的append()、substr()、erase()和insert()等函数对字符串进行处理。
在代码中,首先定义了字符串变量str1和str2,并将其初始化为hello和lyshark,然后使用append()函数将字符串str2的内容追加到字符串str1的末尾,并输出追加后的结果。
使用append()函数将字符串str2的第1个字符到第3个字符之间的内容追加到字符串str1的末尾,并输出追加后的结果。
定义新的字符串变量str3,并将其初始化为"this is ok",然后使用substr()函数取子串,并将截取出来的子串赋值给变量str4和str5。
定义新的字符串变量str6,并将其初始化为"real steel",然后使用erase()函数从第5个字符开始向后删除所有字符,并输出删除后的结果。然后,使用erase()函数从第0个字符开始向后删除4个字符,并输出删除后的结果。
定义新的字符串变量str7,并将其初始化为"hello lyshark",然后使用insert()函数在下标2处插入字符串"123",并使用insert()函数在下标3处插入4个字符A,并输出插入后的结果。
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string str1("hello "), str2("lyshark");
str1.append(str2); // 将str2的内容追加到str1后面
str1.append(str2, 1, 3); // 将str2内容从第1个到第3个字符追加到str1后面
str1.append(5, 'A'); // 向str1子串里面追加5个A
string str3 = "this is ok";
string str4 = str3.substr(1,3); // 截取子串 => his
string str5 = str3.substr(1, 5); // 截取子串 => his i
string str6 = "real steel";
str6.erase(5); // 从第5个字符开始向后删除所有
str6.erase(0, 4); // 从第0个字符开始向后删除4个
string str7 = "hello lyshark";
str7.insert(2, "123"); // 在下标 2 处插入字符串"123"
str7.insert(3, 4, 'A'); // 在下标 3 处插入 5 个 'A'
system("pause");
return 0;
}如下C++代码,展示了如何使用STL字符串中的find()、substr()、find_first_of()、compare()和replace()等函数对字符串进行处理。
使用find()函数在字符串str1中查找字符u第一次出现的位置,并将其赋值给变量x,如果查找到了,则使用substr()函数输出从字符u位置到字符串结尾处的子串。
使用find()函数在字符串str1中查找字符串"Source",并从下标3的位置开始查找,在找到的位置处使用substr()函数输出从该位置开始到字符串结尾处的子串。
使用find_first_of()函数在字符串str1中查找字符串"urc"中的第一个出现的字符,并将其位置输出。
使用compare()函数比较两个字符串变量str1和str2是否相等,如果不相等,则输出False。
最后,定义了一个新的字符串变量str3,并将其初始化为"hello lyshark",然后使用replace()函数从第1个位置开始替换3个字符并将其替换成"abcde"。
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string str1("Source Code"),str2("Source Code");
int x;
if ((x = str1.find("u")) != string::npos)
cout << str1.substr(x) << endl; // 查找字符u第一次出现的位置
if ((x = str1.find("Source", 3)) != string::npos)
cout << str1.substr(x) << endl; // 查找Source字符串,并从下标3位置输出
if ((x = str1.find_first_of("urc")) != string::npos)
cout << x << endl; // 查找urc字符串最先出现的位置
if (str1.compare(str2)) // 比较两个字符串是否相等
cout << "False" << endl;
string str3 = "hello lyshark";
str3.replace(1, 3, "abcde"); // 从第1处开始替换3个字符
cout << str3 << endl;
system("pause");
return 0;
}如下C++代码,展示了如何使用STL字符串string类型中的substr()函数和find()函数将字符串分解为多个子串,并输出提取后的结果。
使用find()函数查找字符@在字符串email中的位置,并将其赋值给变量pos。
使用substr()函数提取从字符串email的第0个字符开始到pos位置之前的子串,并将其赋值给变量username,并输出该变量的值。
使用substr()函数提取从字符串email的pos+1位置开始到结束的子串,并将其赋值给变量mail,并输出该变量的值。
读者需要注意,在使用substr()函数提取子串时,第一个参数表示子串的起始位置,第二个参数表示子串的长度。如果第二个参数不写,则默认提取从起始位置开始到字符串结尾处的字符。
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string email = "admin@lyshark.cn";
int pos = email.find("@");
string username = email.substr(0, pos); // 提取出用户名
cout << username << endl;
string mail = email.substr(pos+1); // 提取出mail邮箱
cout << mail << endl;
system("pause");
return 0;
}如下C++代码,展示了如何使用STL字符串string类型和标准库函数实现了不同类型之间的相互转换,包括string转换为char类型,char转换为string类型,以及int转换为string类型。
使用c_str()函数将string类型的变量转换为const char*类型,并将其存储到指针变量ptr中,用于输出其值。
使用指针变量ptr创建一个新的string类型的变量str1,并将转换后的字符串赋值给它,用于输出其值。
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string str = "lyshark";
// string 转换为 -> char *
const char *ptr = str.c_str();
cout << ptr << endl;
// char * 转换为 -> string
string str1(ptr);
cout << str1 << endl;
// int 转换为 -> string
int Num = 546;
string str2 = to_string(Num);
cout << str2 << endl;
system("pause");
return 0;
}如下C++代码,展示了如何使用标准库函数toupper()将字符串中的字母全部转换为大写形式,并输出转换后的结果。
使用toupper()函数将每个字符转换成大写形式,并将转换后的字符再存储回原始的字符串变量str中。
最后,输出字符串中所有的字母字符均已转换成大写形式的结果。
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string str = "lyshark";
for (int x = 0; x < str.size(); x++)
str[x] = toupper(str[x]); // 全部变大写
//str[x] = tolower(str[x]); // 全部变小写
cout << str << endl;
system("pause");
return 0;
}本文作者: 王瑞
本文链接: https://www.lyshark.com/post/9ab96ef3.html
版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!