标准C++中string类的用法

包含头文件为:

#include <string>

声明命名空间:

using  std::string;
using  std::wstring;


using namespace std;
就可以使用string/wstring了,它们两分别对应着char和wchar_t,string和wstring的用法是一样的。

定义、初始化

string类有多种定义和初始化方式,可以用字符串常量或者c语言字符串数组或者string对象初始化新的string对象。

string str1("My name is apple");
string str2 = "I like football";
string str3(str1, 3);  //"name is apple"
string str4(str1, 3, 4);  //"name"

char chr[] = "string is good";
string str5(chr);  //"string is good"
string str6 = chr;  //"string is good"
string str7(chr, 3);  //"str"
string str8(chr, 3, 2);  //"in"

string str9(4,'d');  //"dddd"

属性描述

string str1("My name is apple");
int i = str1.capacity();//返回当前容量(即string中不必增加内存即可存放的元素个数)
double long j = str1.max_size();//返回string对象中可存放的最大字符串的长度
int iSize = str1.size();//返回值:16。返回当前字符串的大小(单位:字节)
int iLen = str1.length();//返回值:16。与size()函数功能一样
bool bIsEmpty = str1.empty();//返回值:false。判断字符串是否为空

赋值操作

string &operator=(const string &s); //把字符串s赋给当前字符串
string &assign(const char *s);      //用c语言字符串s赋值
string &assign(const char *s,int n);//用c语言字符串s开始的n个字符赋值
string &assign(const string &s);  //把字符串s赋给当前字符串
string &assign(int n,char c);     //用n个字符c赋值给当前字符串
string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串
string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串

访问字符串

//返回pos开始的n个字符组成的字符串
string substr(int pos = 0,int n = npos) const;

string str1("Hello");
cout << str1[1] <<endl;//输出e
cout << str1.at(1) << endl;//输出e

插入、链接操作

在末尾插入一个字符:

str.push_back('a');//在str末尾添加一个字符'a'

在末尾链接字符串:

string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾 
string &append(const char *s);  //把字符串s连接到当前字符串结尾
string &append(const char *s,int n);//把字符串s的前n个字符连接到当前字符串结尾
string &append(const string &s);    //同operator+=()
string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾
string &append(int n,char c);        //在当前字符串结尾添加n个字符c
string &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾

在中间插入字符串:

string &insert(int p0, const char *s);//将c语言字符串s插入当前字符串的p0位置
string &insert(int p0, const char *s, int n);//将c语言字符串s的前n个字符插入当前字符串的p0位置
string &insert(int p0,const string &s);//将字符串s插入当前字符串的p0位置
string &insert(int p0,const string &s, int pos, int n);//将c语言字符串s中pos开始的前n个字符,插入当前字符串的p0位置
string &insert(int p0, int n, char c);//在当前字符串的p0处插入n个字符c
iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符
void insert(iterator it, int n, char c);//在it处插入n个字符c

例如:

string str1("Hello,");
string str2("My name is apple.");
string str;
str = str1 + str2;//"Hello,My name is apple."
str1 += str2;//"Hello,My name is apple."
str2.append("hey!");//"Hello,My name is apple.hey!"

替换操作

//将当前字符串从p0开始的n0个字符,替换为字符串s 
string &replace(int p0, int n0,const string &s);
string &replace(int p0, int n0,const char *s);
//将当前字符串从p0开始的n0个字符,替换为s中从pos开始的n个字符
string &replace(int p0, int n0,const string &s, int pos, int n);
//将当前字符串从p0开始的n0个字符,替换为字符串s的前n个字符
string &replace(int p0, int n0,const char *s, int n);


//将当前字符串从p0开始的n0个字符,替换为n个字符c
string &replace(int p0, int n0,int n, char c);

//把[first0,last0)之间的部分替换为字符串s
string &replace(iterator first0, iterator last0,const char *s);
//把[first0,last0)之间的部分替换为s的前n个字符
string &replace(iterator first0, iterator last0,const char *s, int n);
//把[first0,last0)之间的部分替换为串s
string &replace(iterator first0, iterator last0,const string &s);
//把[first0,last0)之间的部分替换为n个字符c
string &replace(iterator first0, iterator last0,int n, char c);
//把[first0,last0)之间的部分替换成[first,last)之间的字符串
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);

交换操作

void swap(string &s2);    //交换当前字符串与s2的值

比较操作

bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等。运算符">","<",">=","<=","!="均被重载用于字符串的比较;
//compare函数在>时返回1,<时返回-1,==时返回0 
int compare(const string &s) const;//比较当前字符串和s的大小
int compare(const char *s) const;//比较当前字符串和c语言字符串s的大小
int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int compare(int pos, int n,const char *s) const;
int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小
int compare(int pos, int n,const char *s, int pos2) const;

查找操作

//成功时返回所在位置,失败返回string::npos的值
//向后查找。 
int find(char c, int pos = 0) const; //从pos开始查找字符c在当前字符串的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
 
//向前查找
int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const string &s,int pos = npos) const;
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;

int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
int find_first_of(const string &s,int pos = 0) const;
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;

int find_first_not_of(char c, int pos = 0) const;//从当前串中pos开始查找第一个不是字符c的位置
int find_first_not_of(const string &s,int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;

//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找
int find_last_of(char c, int pos = npos) const;
int find_last_of(const string &s,int pos = npos) const; 
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const string &s,int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;

删除操作

//删除pos开始的n个字符,返回修改后的字符串
string &erase(int pos = 0, int n = npos);
//删除[first,last)之间的所有字符,返回删除后迭代器的位置
iterator erase(iterator first, iterator last);
//删除it指向的字符,返回删除后迭代器的位置
iterator erase(iterator it);





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值