string class操作

C++ 使用 string 类处理字符串

1.string类中的函数

(1) 构造 (string)
(2) 追加 (append)
(3) 赋值 (assign)
(4) 位置与清除(at,clear)
(5) 长度与容量 (size,length)
(6) 比较 (compare)
(7) 子 串 (substr)
(8) 搜索 (find)
(9) 运算符
(看下面表)

2. 注意事项

操作string对象中的字符串内容时,有时会用到“index”。
c++中string不以\0为结束标志

很多string的函数接受两个数字参数: index, n

(1) index: 从index号位置开始

(2) n: 之后的n个字符

3. Constructing a String (创建 string 对象)

Create an empty string using string’s no-arg constructor(用无参构造函数创建一个空字串):

string newString;

Create a string object from a string value or from an array of characters (由一个字符串常量或字符串数组创建string对象) :

string message{ “Aloha World!” };

char charArray[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}; //注意:此处必须有结束符\0不然字符串没有结束

string message1{ charArray };

4. Appending a String (追加字符串)

You can use several overloaded functions to add new contents to a string. (一系列的重载函数可以将新内容附加到一个字符串中)

string s1{ "Welcome" };

s1.append( " to C++" ); // appends " to C++" to s1

cout << s1 << endl; // s1 now becomes Welcome to C++

string s2{ "Welcome" };

s2.append( " to C and C++", 3, 2 ); // appends " C" to s2

cout << s2 << endl; // s2 now becomes Welcome C

string s3{ "Welcome" };

s3.append( " to C and C++", 5); // appends " to C" to s3

cout << s3 << endl; // s3 now becomes Welcome to C

string s4{ "Welcome" }; 

s4.append( 4, 'G' ); // appends "GGGG" to s4

cout << s4 << endl; // s4 now becomes WelcomeGGGG

5. Assigning a String (为字符串赋值)

You can use several overloaded functions to assign new contents to a string(一系列的重载函数可以将一个字符串赋以新内容)

string s1{ "Welcome" };

s1.assign( "Dallas" ); // assigns "Dallas" to s1

cout << s1 << endl; // s1 now becomes Dallas

string s2{ "Welcome" };

s2.assign( "Dallas, Texas", 1, 3 ); // assigns "all" to s2

cout << s2 << endl; // s2 now becomes all

string s3{ "Welcome" };

s3.assign( "Dallas, Texas", 6 ); // assigns "Dallas" to s3

cout << s3 << endl; // s3 now becomes Dallas

string s4{ "Welcome" };

s4.assign( 4, 'G' ); // assigns "GGGG" to s4

cout << s4 << endl; // s4 now becomes GGGG

6. Functions at, clear, erase, and empty

(1) at(index): 返回当前字符串中index位置的字符

(2) clear(): 清空字符串

(3) erase(index, n): 删除字符串从index开始的n个字符

(4) empty(): 检测字符串是否为空


string s1{ "Welcome" };

cout << s1.at(3) << endl; // s1.at(3) returns c

cout << s1.erase(2, 3) << endl; // s1 is now Weme

s1.clear(); // s1 is now empty

cout << s1.empty() << endl; // s1.empty returns 1 (means true)

 

7. Comparing Strings (比较字符串)

compare() 函数用于比较两个字符串。它与C语言中的 strcmp() 函数很像。

string s1{ "Welcome" };

string s2{ "Welcomg" };

cout << s1.compare(s2) << endl; // returns -2

cout << s2.compare(s1) << endl; // returns 2

cout << s1.compare("Welcome") << endl; // returns 0

8. Obtaining Substrings (获取子串)

at() 函数用于获取一个单独的字符;

substr() 函数则可以获取一个子串

 

string s1{ "Welcome" };

cout << s1.substr(0, 1) << endl; // returns W;  从0号位置开始的1个字符

cout << s1.substr(3) << endl; // returns come;  从3号位置直到末尾的子串

cout << s1.substr(3, 3) << endl; // returns com;从3号位置开始的3个字符

 

9. Searching in a String (搜索字符串)

find() 函数可以在一个字符串中搜索一个子串或者一个字符

string s1{ "Welcome to C++" };

cout << s1.find("co") << endl; // returns 3; 返回子串出现的第一个位置

cout << s1.find("co", 6) << endl; // returns -1 从6号位置开始查找子串出现的第一个位置

cout << s1.find('o') << endl; // returns 4    返回字符出现的第一个位置

cout << s1.find('o', 6) << endl; // returns 9   从6号位置开始查找字符出现的第一个位置

 

10. Inserting and Replacing Strings (插入和替换字符串)
insert() : 将某个字符/字符串插入到当前字符串的某个位置

replace() 将本字串从某个位置开始的一些字符替换为其它内容

 

string s1("Welcome to C++");

s1.insert(11, "Java and ");

cout << s1 << endl; // s1 becomes Welcome to Java and C++

string s2{ "AA" };

s2.insert(1, 4, 'B'); //在1号位置处连续插入4个相同字符

cout << s2 << endl; // s2 becomes to ABBBBA

string s3{ "Welcome to Java" };

s3.replace(11, 4, "C++"); //从11号位置开始向后的4个字符替换掉。注意'\0'

cout << s3 << endl; // returns Welcome to C++ 
  1. String Operators (字符串运算符)

在这里插入图片描述

string s1 = "ABC"; // The = operator

string s2 = s1;    // The = operator

for (int i = s2.size() - 1; i >= 0; i--)

    cout << s2[i]; // The [] operator

 

string s3 = s1 + "DEFG"; // The + operator

cout << s3 << endl; // s3 becomes ABCDEFG

 

s1 += "ABC";

cout << s1 << endl; // s1 becomes ABCABC

 

s1 = "ABC";

s2 = "ABE";

cout << (s1 == s2) << endl; // Displays 0 

cout << (s1 != s2) << endl; // Displays 1 

cout << (s1 >  s2) << endl; // Displays 0 

cout << (s1 >= s2) << endl; // Displays 0 

cout << (s1 <  s2) << endl; // Displays 1 

cout << (s1 <= s2) << endl; // Displays 1 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值