C++的string类

C语言中char[] 的⽅式处理字符串很繁琐,C++中有优化后的string 类,
不过 string 只能⽤ cin 和 cout 处理,⽆法⽤ scanf 和 printf 处理,样例:
string s = “hello world”; // 赋值字符串
string s2 = s;
string s3 = s + s2; // 字符串拼接直接⽤+号就可以
string s4;
for(int i = 0;i < s.length();i++)cout << s[i];//下标访问单字符
cin >> s4; // 读⼊字符串,遇到空格结束
getline(cin, s4); // 读入整行字符串,可以包含空格
cout << s; // 输出字符串
⽤ cin 读⼊字符串的时候,是以空格为分隔符的,如果想要读⼊⼀整⾏的字符串,就需要⽤ getline。
s 的⻓度可以⽤ s.length() 获取,有⼏个字符就是⻓度多少,不存在什么末尾的’/0’的情况。
string 中还有个很常⽤的函数叫做 substr ,作⽤是截取某个字符串中的⼦串,⽤法有两种形式:
string s2 = s.substr(4); // 表示从下标4开始⼀直到结束
string s3 = s.substr(5, 3); // 表示从下标5开始,3个字符

//字符与整数的联系—ASCII码
每个常用字符都对应一个-128~127之间的数字,二者之间可以相互转换。
常用ASCII值:‘A’'Z’是6590,‘a’'z’是97122,‘0’'9’是4857。
字符可以参与运算,运算时会将其当做整数处理。

// 创建字符串
way1: string s(“hello”); // 将string对象初始化为s指向的字符串
way2: string s; // 只声明,不初始化
way3: string s = “hello”; // 声明的同时并初始化
way4: string s(10, ‘a’); //创建一个包含10个元素的string对象,每个元素被初始化为a

// 定义字符串数组
string s[n]; // 数组s含n个字符串元素
cout << s[i][j]; // 输出第i个字符串的第j个元素
cout << s[i]; // 输出字符串i

// 读入字符串
cin >> s; // 如果字符串中没有空格,选择cin输入。如果输入的字符串开头为空格符、制表符和回车符时会忽略这些符号,从后续字符作为开始输入,以空格符、制表符和回车符作为结束标志
getline(cin, s); // 如果字符串中有空格,选择getline()输入。以回车符作为结束标志

// 获取字符串的长度
way1: int len = s.length();
way2: int len = s.size();

// 遍历字符串
for(auto it : s) // 第一种方式
for(int i = 0; i < s.length(); i++) // 第二种方式
cout << s[i];

// 输出字符串
cout << s << endl;

// 将一个string对象赋给另一个string对象
string s1(“hello”);
string s2 = s1; // s2 = “hello”

// 大小写转换
//使用位运算进行字符大小写转换
char zimu = ‘a’;
zimu ^= (1 << 5);//位运算yyds

// toupper() 和 tolower() 函数对字符进行转换
if (ch >= ‘a’ && ch <= ‘z’) ch = toupper(ch);
if (ch >= ‘A’ && ch <= ‘Z’) ch = tolower(ch);

// transform()函数进行字符串的大小写转换
transform(str.begin(), str.end(), str.begin(), toupper); // 字符串整体转为大写
transform(str.begin(), str.end(), str.begin(), tolower); // 字符串整体转为小写

// to_string函数将数字转换成字符串
int x = 12345;
string s = to_string(x); // s = “12345”
double x = 12345.6;
string s = to_string(x); // s = “12345.6”

// stoi stol stod stof 等函数将字符串转换为数字
string s = “115.5XY”;
int x = stoi(s); // x = 115; // 将字符串转换为整形数字
int x2 = stof(s); // x2 = 115.5; // 将字符串转换为浮点型数字

// string对象的拼接
string s1("hello ");
string s2(“world”);
way1: string s3 = str1 + str2; // s3 = “hello world”
way2: string s1 += “world”; // s1 = “hello world”
way3: s1.append(“world”); // s1 = “hello world”

// string对象的后面添加一个字符
string s(“hello”);
way1: s.push_back(‘a’); // s = “helloa”
way2: s += ‘a’;

// string对象的比较
string s1 = “hello world”;
string s2 = “hello boy”;
== 相同返回1,不同返回0
way1: if(s1 == s2)
s.compare()相同返回0,不同返回1
str1.compare(str2); // 字符串的比较
str1.compare(3,4,str2); // str1的子串和str2比较
way2: s1.compare(6, 3, s2, 6, 3); // compare(s1起始位置, 长度, s2, s2起始位置, 长度)

// string.substr()来获取子串
string s(“hello”);
string s2 = s.substr(3, 2); // s2 = “lo”;
s.substr();//返回s的全部内容
s.substr(11);//从索引11往后的子串
s.substr(5,6);//从索引5开始6个字符

// 访问字符串元素
string s(“hello”);
way1: cout << str[1] << end; // 输出字符e
way2: cout << s.at(1) << endl; // 输出字符e

// string.find()查找字符/字符串,若找到,返回该子字符串首次出现时其首字母的索引。找不到返回-1
string s1(“hello world”);
string s2(“o w”);
int first_index = s1.find(s2, 0); // s1从位置0开始查找子串s2,返回子串首次出现的首位置

// string.rfind()与string.find()查找顺序不同,返回该子字符串最后一次出现时其首字母的索引

// str.find_first_of() 方法在字符串str中从指定位置开始向后(默认为索引 0 处)查找参数中任何一个字符首次出现的位置
string s(“hello world”);
int pos = s.find_first_of(“abcde”); // 字符e在s中,返回e在s中首次出现的位置

// string.find_last_of() 方法在字符串中查找参数中任何一个字符最后一次出现的位置
// string.find_first_not_of() 方法在字符串中查找第一个不包含在参数中的字符
// string.find_last_not_of() 方法在字符串中查找最后一个不包含在参数中的字符

// string.insert()进行插入操作
在位置 pos 处插入字符串: strstring& insert(size_t pos,const string&str);
在位置 pos 处插入字符串s: string& insert(size_t pos,const char * s);
s.insert(i,s2); //将字符串s2插入到字符串s1的位置i处
在位置 pos 处插入字符串 str 的从位置 subpos 处开始的 sublen 个字符:
string& insert(size_t pos,const string&str,size_t subpos,size_t sublen);
string& insert(size_t pos,const char * s,size_t n);// 在位置 pos 处插入字符串 s 的前 n 个字符
string& insert(size_t pos,size_t n,char c); // 在位置 pos 处插入 n 个字符 c
iterator insert (const_iterator p, size_t n, char c); // 在 p 处插入 n 个字符 c,并返回插入后迭代器的位置
iterator insert (const_iterator p, char c); // 在 p 处插入字符 c,并返回插入后迭代器的位置

// string.erase() 进行元素删除操作
string& erase (size_t pos = 0, size_t len = npos);   // 删除从 pos 处开始的 n 个字符
s.erase(i, n); // 删除字符串s中从位置i开始的n个字符
iterator erase (const_iterator p);  // 删除 p 处的一个字符,并返回删除后迭代器的位置
iterator erase (const_iterator first, const_iterator last); // 删除从 first 到last 之间的字符,并 返回删除后迭代器的位置

// string.empty() 函数判断字符串是否为空

// string.swap() 函数交换两个字符串
string s1 = “hello”;
string s2 = “HELLO”;
s1.swap(s2); // s1 = “HELLO”, s2 = “hello”

// string.back() 获取字符串最后一个字符
string str(“abcd”);
char b = str.back();
str.back() = ‘e’; // 修改字符串最后一个字符

// string.front() 获取字符串第一个字符

// string.pop_back() 删除字符串最后一个元素

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值