跳到正文

个人记录

STL之string

liaochengrui约 1 分钟
原文
文章信息与版本

文章信息

发布
2023年10月14日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 1 分钟

版本

  1. v1当前

字符串string

调用

C++
#include<string>
string s;

用法

  • s1+s2 :连接两个字符串。

  • s.size() :返回字符串长度。

  • s.find(t) :返回 tt 在 ss 中第一次出现的位置,没有则返回 −1-1 ( tt 可以为字符或字符串)。

  • s.substr(pos,len) :返回从 pospos 位置开始截取最多 lenlen 个字符组成的字符串(如果从 pospos 开始的后缀长度不足 lenlen 则截取这个后缀)。

  • insert(index,count,ch) 和 insert(index,str) 分别表示在 indexindex 处连续插入 countcount 次字符串 chch 和插入字符串 strstr 。

  • erase(index,count) 表示将字符串 indexindex 位置开始(含)的 countcount 个字符删除(若不传参给 countcount 则表示删去 indexindex 位置及以后的所有字符)。

  • replace(pos,count,str) 和 replace(first,last,str) 分别表示将从 pospos 位置开始 countcount 个字符的子串替换为 strstr 以及将以 firstfirst 开始(含)、 lastlast 结束(不含)的子串替换为 strstr。

不是很详细,不要介意

来源:OI WIKI

原文