在本章中,我们将学习以下主题:
- 介绍
- C ++中的字符串对象
- 字符串关系运算符
- 字符串修改功能
- 字符串属性函数
- 访问字符串元素
- 比较字符串函数
1.简介
字符串是字符序列。每个字符串都应以NULL字符“ \ 0”结尾。在C样式中,需要分配额外的空间来容纳NULL值。
C风格的字符串:
char website_name [21] = “prodevelopertutorial”;
C ++提供了“串”,使字符串操作变得容易。
2. C ++中的字符串对象
“串”是用于声明字符串变量的关键字。
以下是有关如何使用字符串的各种示例:
串 text; //Creates an empty 串 string language("c++"); //assigns "c++" to the variable language string_1 = 串_2 //assign value of 串_2 to 串_1 string_1 = "c++" + "tutorils" // 串 concatenation
C ++中的字符串用法示例
/* * File : 串s_example.cpp * Author : [email protected] * Copyright: @ 前开发者教程.com */ #include<iostream> #include<string> using namespace std; int main() { 串 串_1 = "c++"; 串 串_2(" 前开发者教程"); 串 串_3; cout<<"The 串_1 is "<<string_1<<endl; cout<<"The 串_2 is "<<string_2<<endl; //string concatenation 串_1 += 串_2; cout<<"The 串_1 after concatenation is \""<<string_1<<"\""<<endl; //string assignment 串_3 = 串_2; cout<<"The 串_3 after assignment is "<<string_3<<endl; return 0; }
输出:
The 串_1 is c++ The 串_2 is 前开发者教程 The 串_1 after concatenation is "c++ 前开发者教程" The 串_3 after assignment is 前开发者教程
3.字符串关系运算符
我们可以使用关系运算符(如下所示)来比较两个字符串是否相同。
== 操作员检查两个字符串是否相同
> 检查string_1是否大于string_2
关系运算符的示例:
/* * File : 串s_relational_operator_example.cpp * Author : [email protected] * Copyright: @ 前开发者教程.com */ #include<iostream> #include<string> using namespace std; int main() { 串 串_1 = "c++"; 串 串_2("c++"); 串 串_3 = "C ++"; cout<<"The 串_1 is = "<<string_1<< " and 串_2 is = "<<string_2<<" and 串_1 == 串_2 value is = "<< (string_1 == 串_2) <<endl; cout<<"The 串_1 is = "<<string_1<< " and 串_3 is = "<<string_3<<" and 串_1 == 串_3 value is = "<< (string_1 == 串_3) <<endl; cout<<"The 串_1 is = "<<string_1<< " and 串_2 is = "<<string_2<<" and 串_1 > 串_2 value is = "<< (string_1 > 串_2) <<endl; cout<<"The 串_1 is = "<<string_1<< " and 串_3 is = "<<string_3<<" and 串_1 > 串_3 value is = "<< (string_1 > 串_3) <<endl; return 0; }
输出:
The 串_1 is = c++ and 串_2 is = c++ and 串_1 == 串_2 value is = 1 The 串_1 is = c++ and 串_3 is = C ++ and 串_1 == 串_3 value is = 0 The 串_1 is = c++ and 串_2 is = c++ and 串_1 > 串_2 value is = 0 The 串_1 is = c++ and 串_3 is = C ++ and 串_1 > 串_3 value is = 1
4.字符串修改功能
以下是用于修改字符串的函数。
- 插入() 函数用于在指定位置插入字符串。
句法:
串_1.insert(<position>, <string_to_insert>);
- 擦除() 功能用于删除指定的字符。
句法:
串_1.insert(<position_start>, <position_end>);
- 更换() 函数用于替换指定的字符。
句法:
串_1.insert(<position_start>, <position_end>, <string_2>);
- 附加() 函数用于在第一个字符串的末尾附加另一个字符串。
句法:
串_1.append(string_2);
字符串修改功能示例:
/* * File : 串s_modification_example.cpp * Author : [email protected] * Copyright: @ 前开发者教程.com */ #include<iostream> #include<string> using namespace std; int main() { 串 串_1 = "c++ "; 串 串_2 = "Tutorial"; 串 串_3 = "前开发者教程.com"; 串 串_4 = "Tutorial 上 前开发者教程"; 串 串_5 = "c++ "; cout<<"The 串_1 is = "<<string_1<< " and 串_2 is = "<<string_2<<" and 串_1.insert(4, 串_2) value is = "<<(string_1.insert(4, 串_2)) <<endl; cout<<"The 串_3 is = "<<string_3<< " and 串_3.erase(20, 24) value is = "<<(string_3.erase(20, 24)) <<endl; cout<<"The 串_3 is = "<<string_3<< " and 串_3.replace(3, 9, \"DEVELOPER\") value is = "<<(string_3.replace(3, 9, "DEVELOPER")) <<endl; cout<<"The 串_5 is = "<<string_5<< " and 串_4 is = "<<string_4<<" and 串_5.append(string_4) value is = "<<(string_5.append(string_4)) <<endl; return 0; }
输出:
The 串_1 is = c++ and 串_2 is = Tutorial and 串_1.insert(4, 串_2) value is = c++ Tutorial The 串_3 is = 前开发者教程.com and 串_3.erase(20, 24) value is = 前开发者教程 The 串_3 is = 前开发者教程 and 串_3.replace(3, 9, "DEVELOPER") value is = proDEVELOPERtutorial The 串_5 is = c++ and 串_4 is = Tutorial 上 前开发者教程 and 串_5.append(string_4) value is = c++ Tutorial 上 前开发者教程
5.字符串属性函数
以下是用于获取字符串属性的函数。
- 尺寸() 函数用于获取字符串对象的大小。它给出了该字符串对象占用的字节数。
句法:
串_1.size()
- 长度() 函数用于获取字符串对象的长度。它给出了该字符串对象中存在的字符数。
句法:
串_1.length()
- 容量() 函数用于获取字符串对象的容量。
句法:
串_1.capacity()
- max_size() 函数用于获取字符串对象的最大大小。
句法:
串_1.max_size()
字符串属性函数的示例:
/* * File : 串s_attributes_example.cpp * Author : [email protected] * Copyright: @ 前开发者教程.com */ #include<iostream> #include<string> using namespace std; int main() { 串 串_1; cout<<"The value of 串_1 is = "<<string_1<<" and size of 串_1 is = "<<string_1.size()<<endl; 串_1 = "C ++"; cout<<"The value of 串_1 is = "<<string_1<<" and size of 串_1 is = "<<string_1.size()<<endl<<endl; 串 串_2; cout<<"The value of 串_2 is = "<<string_2<<" and length of 串_2 is = "<<string_2.length()<<endl; 串_2 = "C ++"; cout<<"The value of 串_2 is = "<<string_2<<" and length of 串_2 is = "<<string_2.length()<<endl<<endl; 串 串_3; cout<<"The value of 串_3 is = "<<string_3<<" and capacity of 串_3 is = "<<string_3.capacity()<<endl; 串_3 = "C ++"; cout<<"The value of 串_3 is = "<<string_3<<" and capacity of 串_3 is = "<<string_3.capacity()<<endl<<endl; 串 串_4; cout<<"The value of 串_4 is = "<<string_4<<" and max_size of 串_4 is = "<<string_4.max_size()<<endl; return 0; } 输出:
The value of 串_1 is = and size of 串_1 is = 0 The value of 串_1 is = C ++ and size of 串_1 is = 3 The value of 串_2 is = and length of 串_2 is = 0 The value of 串_2 is = C ++ and length of 串_2 is = 3 The value of 串_3 is = and capacity of 串_3 is = 22 The value of 串_3 is = C ++ and capacity of 串_3 is = 22 The value of 串_4 is = and max_size of 串_4 is = 18446744073709551599
6.访问字符串元素
1. 在(): 函数用于获取该索引处的字符。
例:
串_1 = “c++” string_1.at(0) will return “c”
2. substr() 函数用于获取从“ start_index”到“ end_index”的子字符串。
例:
串_1. substr (2, 4)
3. find() 函数用于检查“字符串”中是否存在“子字符串”。
例:
串_1. find (“hi”)
4. find_first_of() :用于查找给定字符的第一个匹配项。
例:
串_1. find_first_of (“/”)
5. find_last_of() :用于查找给定字符的最后一次出现。
例:
串_1. find_last_of (“/”)
字符串访问的示例:
/* * File : accessing_strings_example.cpp * Author : [email protected] * Copyright: @ 前开发者教程.com */ #include<iostream> #include<string> using namespace std; int main() { 串 串_1 = "前开发者教程"; cout<<"The value of 串_1 is = "<<string_1<<" and the character at 2 is = "<<string_1.at(2)<<endl; 串 串_2 = "c++ tutorial at 前开发者教程"; cout<<"The value of 串_2 is = "<<string_2<<" and find(\" tutorial\") is = "<<string_1.find("tutorial")<<endl; cout<<"The value of 串_1 is = "<<string_1<<" and substr(3, 11) is = "<<string_1.substr(3, 11)<<endl; 串 串_3 = "a/b/c"; cout<<"The value of 串_3 is = "<<string_3<<" and find_first_of(\"/\") is = "<<string_3.find_first_of("/")<<endl; cout<<"The value of 串_3 is = "<<string_3<<" and find_last_of(\"/\") is = "<<string_3.find_last_of("/")<<endl; return 0; }
输出:
The value of 串_1 is = 前开发者教程 and the character at 2 is = o The value of 串_2 is = c++ tutorial at 前开发者教程 and find(" tutorial") is = 12 The value of 串_1 is = 前开发者教程 and substr(3, 11) is = developertu The value of 串_3 is = a/b/c and find_first_of("/") is = 1 The value of 串_3 is = a/b/c and find_last_of("/") is = 3
7.比较字符串函数
1. 比较() 函数用于比较2个字符串。
2. 交换() 函数用于交换2个字符串。
例:
/* * File : compare_and_swap_strings_example.cpp * Author : [email protected] * Copyright: @ 前开发者教程.com */ #include<iostream> #include<string> using namespace std; int main() { 串 串_1 = "前开发者教程"; 串 串_2 = "前开发者教程"; 串 串_3 = "Prodevelopertutorial"; cout<<"The 串_1 is = "<<string_1<< " and 串_2 is = "<<string_2<<" and 串_1.compare( 串_2) value is = "<<(string_1.compare( 串_2)) <<endl; cout<<"The 串_1 is = "<<string_1<< " and 串_3 is = "<<string_3<<" and 串_1.compare( 串_3) value is = "<<(string_1.compare( 串_3)) <<endl; 串 串_4 = " c++ tutorial"; cout<<"The 串_1 is = "<<string_1<< " and 串_4 is = "<<string_4<<endl; 串_1.swap(string_4); cout<<"After swap"<<endl; cout<<"The 串_1 is = "<<string_1<< " and 串_4 is = "<<string_4<<endl; return 0; }
输出:
The 串_1 is = 前开发者教程 and 串_2 is = 前开发者教程 and 串_1.compare( 串_2) value is = 0 The 串_1 is = 前开发者教程 and 串_3 is = Prodevelopertutorial and 串_1.compare( 串_3) value is = 32 The 串_1 is = 前开发者教程 and 串_4 is = c++ tutorial After swap The 串_1 is = c++
该网站上可用的教程列表:
C编程20+章 | C ++编程80+章 |
100多个编码问题 | 数据结构和算法85+章 |
系统设计20+章 | Shell脚本编写12章 |
4g LTE 60+章节 | 最常见的编码问题 |
5G NR 50+章 | Linux系统编程20+章 |