文章目录

  • ​​C++字符串​​
  • ​​C 风格字符串​​
  • ​​实例1:​​
  • ​​实例2:​​
  • ​​C++ 中的 `String `类​​
  • ​​实例3:​​
  • ​​插入字符串​​
  • ​​实例4​​
  • ​​删除字符串​​
  • ​​实例5​​
  • ​​提取子字符串​​
  • ​​实例6​​
  • ​​字符串查找​​
  • ​​`find()` 函数​​
  • ​​实例7​​
  • ​​`rfind()` 函数​​
  • ​​实例8​​
  • ​​`find_first_of()` 函数​​
  • ​​实例9​​

C++字符串

C++ 提供了以下两种类型的字符串表示形式:

  • C 风格字符串
  • C++ 引入的 ​​string​​ 类类型

C 风格字符串

实例1:

#include <iostream>
using namespace std;

int main(void)
{
char site[7] = {'A', 'B', 'C', 'd', 'E', 'F', '\0'};

cout << "TEST: ";
cout << site << endl;

string str1 = "123456";
string str2 = "abcdefg";
string str = str1 + str2; /* 拼接字符串 */
cout << str << endl; /*输出字符串*/

return 0;
}

编译、运行结果:

PS E:\fly-prj\cplusplus\day2> make 
g++ -o string1 string1.cpp -g -Wall
PS E:\fly-prj\cplusplus\day2> .\string1.exe
TEST: ABCdEF
123456abcdefg

实例2:

#include <iostream>
#include <cstring>
using namespace std;

int main(void)
{
char str1[13] = "ABCDEFG";
char str2[13] = "12345678";
char str3[13];
int len;

// copyt str1 to str3
strcpy(str3, str1); /*复制字符串*/
cout << "strcpy(str3, str1): " << str3 << endl;

// strcat str1 and str2
strcat(str1, str2); /*连接字符串*/
cout << "strcat(str1, str2): " << str1 << endl;

// calculate the length of str1
len = sizeof(str1); /*求字符串在内存中的长度*/
cout << "sizeof(str1): " << len << endl;

return 0;
}

编译、运行:

PS E:\fly-prj\cplusplus\day2> make 
g++ -o string2 string2.cpp -g -Wall
PS E:\fly-prj\cplusplus\day2> .\string2.exe
strcpy(str3, str1): ABCDEFG
strcat(str1, str2): ABCDEFG12345678
sizeof(str1): 13

C++ 中的 ​​String​​类

实例3:

#include <iostream>
#include <string>
using namespace std;

int main(void)
{
string str1 = "ABCDEFGH";
string str2 = "1234567890";
string str3;
int len;

str2 = str1; // copy str1 to str2
cout << "str2: " << str2 << endl;

str3 = str1 + str2; // strcat str1 and str2
cout << "str3: " << str3 << endl;

len = str3.size(); // calculate the length of str3
cout << "len: " << len << endl;

return 0;
}

编译、运行:

PS E:\fly-prj\cplusplus\day2> make 
g++ -o string3 string3.cpp -g -Wall
PS E:\fly-prj\cplusplus\day2> .\string3.exe
str2: ABCDEFGH
str3: ABCDEFGHABCDEFGH
len: 16

插入字符串

string& insert (size_t pos, const string& str);

在位置​​pos​​​处插入字符串​​str​​​。​​insert更多用法参考​

​insert()​​函数的第一个参数有越界的可能,如果越界,则会产生运行时异常。

实例4

/*******************************************************************
* > File Name: string-insert.cpp
* > Create Time: 2021年09月25日 23:08:22
******************************************************************/
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
string str1, str2, str3;
str1 = str2 = "0123456789";
str3 = "aaaa";

str1.insert(5, str3);
cout << str1 << endl;
cout << str2 << endl;
str2.insert(5, "AAA");
cout << str2 << endl;

return 0;
}

编译、运行:

PS D:\study\cplusplus\day9> make
g++ -o string-insert string-insert.cpp -g -Wall -std=c++11 -lpthread
PS D:\study\cplusplus\day9> .\string-insert.exe
01234aaaa56789
0123456789
01234AAA56789

删除字符串

string& erase (size_t pos = 0, size_t len = npos);

从字符串的其实下标​​pos​​​开始删除长度为​​len​​​的子字符串。不指明 ​​len​​​ 的话,将直接删除从 ​​pos​​​ 到字符串结束处的所有字符(此时 ​​len​​​ = ​​str.length​​​ - ​​pos​​)。

待删除字符串最多只能删除到字符串结尾

实例5

/*******************************************************************
* > File Name: string-erase.cpp
* > Create Time: 2021年09月25日 23:25:04
******************************************************************/
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
string str1, str2, str3;
str1=str2=str3= "0123456789";

str2.erase(5);
str3.erase(5, 3);
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;

return 0;
}

编译、运行:

PS D:\study\cplusplus\day9> make
g++ -o string-erase string-erase.cpp -g -Wall -std=c++11 -lpthread
PS D:\study\cplusplus\day9> .\string-erase.exe
0123456789
01234
0123489

提取子字符串

string substr (size_t pos = 0, size_t len = npos) const;

从字符串​​pos​​​处,提取​​len​​个字符作为子字符串。

如果​​pos​​​越界,会抛出异常;而​​len​​越界,会提取到结尾处的所有字符。

实例6

/*******************************************************************
* > File Name: string-substr.cpp
* > Create Time: 2021年09月26日 21:21:18
******************************************************************/
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
string s1 = "first second third";
string s2;

s2 = s1.substr(6, 6);// 从位置6开始提取6个字符
cout << "s1: " << s1 << endl;
cout << "s2: " << s2 << endl;

return 0;
}

编译、运行:

PS D:\study\cplusplus\day9> make
g++ -o string-substr string-substr.cpp -g -Wall -std=c++11 -lpthread
PS D:\study\cplusplus\day9> .\string-substr.exe
s1: first second third
s2: second

字符串查找

​find()​​ 函数

size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;

实例7

/*******************************************************************
* > File Name: string-find.cpp
* > Create Time: 2021年09月26日 21:37:42
******************************************************************/
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
string s1 = "first second third";
string s2 = "second";
int index = s1.find(s2, 5);// 从位置5开始查找

if((long unsigned int)index < s1.length()){
cout << "Find index: " << index << endl;
}else{
cout << "Not found index." << endl;
}

return 0;
}

编译、运行:

PS D:\study\cplusplus\day9> make
g++ -o string-find string-find.cpp -g -Wall -std=c++11 -lpthread
PS D:\study\cplusplus\day9> .\string-find.exe
Find index: 6

​rfind()​​ 函数

实例8

/*******************************************************************
* > File Name: string-rfind.cpp
* > Create Time: 2021年09月26日 21:49:06
******************************************************************/
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
string s1 = "first second third";
string s2 = "second";
int index = s1.rfind(s2, 6);// 最多查找到位置6

if((long unsigned int)index < s1.length()){
cout << "Find index: " << index << endl;
}else{
cout << "Not found index." << endl;
}

return 0;
}

编译、运行:

PS D:\study\cplusplus\day9> make
g++ -o string-rfind string-rfind.cpp -g -Wall -std=c++11 -lpthread
PS D:\study\cplusplus\day9> .\string-rfind.exe
Find index: 6

​find_first_of()​​ 函数

查找子字符串字符串共同具有的​​字符​​在字符串中首次出现的位置。

实例9

/*******************************************************************
* > File Name: string-find_first_of.cpp
* > Create Time: 2021年09月26日 21:59:28
******************************************************************/

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
string s1 = "first second second third";
string s2 = "asecond";
/* 查找s1和s2共同具有的字符's' */
long unsigned int index = s1.find_first_of(s2);

if(index < s1.length()){
cout << "Found at index: " << index << endl;
}else{
cout << "Not found" << endl;
}

return 0;
}

编译、运行:

PS D:\study\cplusplus\day9> make
g++ -o string-find_first_of string-find_first_of.cpp -g -Wall -std=c++11 -lpthread
PS D:\study\cplusplus\day9> .\string-find_first_of.exe
Found at index: 3