STL---String的常用函数整理

**

申明:本文以MOOC北京邮电大学的C++程序设计(面向对象进阶)为资料参考撰写,仅用于学习记录

本文所用初始化方式为C++11中的列表初始化,不明原因报错,还望检查编译环境

**

1.追加字符串
append函数

#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1{ "Welcome" };
s1.append( " to C++" ); // 在s1的末尾添加" to C++"
cout << s1 << endl; // s1变为"Welcome to C++"

string s2{ "Welcome" };
s2.append( " to C and C++", 3, 2 ); //从下标为3的字符开始,取2个字符,追加到s2末尾
cout << s2 << endl; // s2变为"Welcome C"

string s3{ "Welcome" };
s3.append( " to C and C++", 5); //从下标为0的字符开始,取5个字符,追加到s3末尾
cout << s3 << endl; // s3变为"Welcome to C"

string s4{ "Welcome" }; 
s4.append( 4, 'G' ); //s4的末尾追加4个'G'
cout << s4 << endl; // s4变为"WelcomeGGGG"
}

2.为字符串赋值
assign
和上面append的使用方法类似

#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1{ "Welcome" };
s1.assign( "Dallas" ); // 用"Dallas"给s1赋值
cout << s1 << endl; // s1变为"Dallas"

string s2{ "Welcome" };
s2.assign( "Dallas, Texas", 1, 3 ); // 取字符串"Dallas, Texas"从下标1开始的3个字符,给s2赋值
cout << s2 << endl; // s2变为"all"

string s3{ "Welcome" };
s3.assign( "Dallas, Texas", 6 ); // 取字符串"Dallas, Texas"从下标0开始的6个字符,给s3赋值
cout << s3 << endl; // s3变为"Dallas"

string s4{ "Welcome" };
s4.assign( 4, 'G' ); // 用"GGGG"给s4赋值
cout << s4 << endl; // s4变为"GGGG"
}

3.at,clear,erase,empty函数
(1) at(index): 返回当前字符串中index位置的字符

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

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

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

#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1{ "Welcome" };
cout << s1.at(3) << endl; // 返回字符串s1中下标为3的字符'c'
cout << s1.erase(2, 3) << endl; //删除s1中从下标2开始的3个字符,s1变为"Weme"
s1.clear(); // 清空s1
cout << s1.empty() << endl; // 如果s1是空的,输出1,否则输出0
}

4.比较字符串
compare() 函数用于比较两个字符串。它与C语言中的 strcmp() 函数很像。逐个比较ascii码的差值,一有不同就结束比较,返回不同单字符的差值。

#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1 {"Welcome"};
string s2{ "Welcomg" };
cout << s1.compare(s2) << endl; // 返回(s1-s2)的差
cout << s2.compare(s1) << endl; // 返回(s2-s1)的差
cout << s1.compare("Welcome") << endl; //完全相同,返回0
}

5.获取子串
at() 函数用于获取一个单独的字符;
substr() 函数则可以获取一个子串;

#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1 {"Welcome"};
cout << s1.substr(0, 1) << endl; // 返回"W";从0号位置开始的1个字符
cout << s1.substr(3) << endl;    // 返回"come";从3号位置直到末尾的子串
cout << s1.substr(3, 3) << endl; //返回"com";从3号位置开始的3个字符
}

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

#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1{ "Welcome to C++" };
cout << s1.find("co") << endl; // 返回3;返回子串出现的第一个位置
cout << s1.find("co", 6) << endl; // 没找到,返回-1 从6号位置开始查找子串出现的第一个位置
cout << s1.find('o') << endl; //返回4;返回字符出现的第一个位置
cout << s1.find('o', 6) << endl; // 返回9;从6号位置开始查找字符出现的第一个位置
}

7.插入和替换字符串
insert() : 将某个字符/字符串插入到当前字符串的某个位置
replace() 将本字串从某个位置开始的一些字符替换为其它内容

#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("Welcome to C++");
s1.insert(11, "Java and ");//从下标11的地方插入"Java and "
cout << s1 << endl; // s1变为"Welcome to Java and C++"

string s2{ "AA" };
s2.insert(1, 4, 'B'); //在1号位置处连续插入4个相同字符
cout << s2 << endl; // s2变为"ABBBBA"

string s3{ "Welcome to Java" };
s3.replace(11, 4, "C++"); //从11号位置开始向后的4个字符替换掉。注意'\0',第一个参数不能大于'\0'的下标
cout << s3 << endl; // s3变为"Welcome to C++" 
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值