【C++】String类基础使用

构造函数

  1. string s1;创建一个string对象s1,长度为0

  1. string s2(char * s);创建一个string对象s2,且初始化为s指针指向的字符串。

//1、将s2初始化为"HailinHou"字符串
string s2("HailinHou");
string s2="HailinHou";//和第2行代码一样的效果
//2、将S2初始化为c指针指向的字符串
char c[]="aaa";
string S2(c);
  1. string s3(int count,char c);创建一个string对象s3,且初始化为长度为count,数值为c的字符串。

  1. string s4(char * s, int n);创建一个string对象s4,且初始化为s指向字符串的前n个字符。若n大于s的长度,则初始化为s字符串+乱码。

  1. string s5(string & str);创建一个string对象s5,且初始化为 string 对象 str(复制构造函数)

  1. string s6(string & str,int n);创建一个string对象s6,且初始化为str对象从位置n开始到末尾的字符串。

//复制s3全部的字符(s3对象从位置0开始到末尾的字符串)
string s6(s3);
string s6=s3;//和第2行代码一样的效果
//位置从n = 7开始复制字符    
string S6(s3,7);
  1. string s7(char* begin,char* end);创建一个string对象s7,且初始化为从指针begin到end的字符串[begin,end)。

char all[]="aaannkksfjfj";
string s6(all+6,all+10);
string S6(&s3[6],&s3[10]);

字符操作

  • string& insert (int pos, string& str); //pos 表示要插入的位置,也就是下标;str 表示要插入的string字符串。

  • string& insert (int pos, char* s); //pos 表示要插入的位置;str 表示要插入的C风格的字符串

  • string& insert (int pos, string& str, int subpos, int sublen); //pos 表示要插入的位置;从str字符串位置 subpos 开始的 sublen 个字符(或者直到 str 的末尾,如果 str 太短或 sublen 太大)

  • iterator insert (iterator p, char c); //

  • string& insert (size_t pos, size_t n, char c); //

  • void insert (iterator p, size_t n, char c); //

  • string& insert (size_t pos, const char* s, size_t n); //

  • string& insert (size_t pos, const string& str, size_t subpos, size_t sublen); //

  • template <class InputIterator> void insert (iterator p, InputIterator first, InputIterator last); //

以下所有的string查找函数,都有唯一的返回类型,那就是size_type,即一个无符号整数(按打印出来的算)。若查找成功,返回按查找规则找到的第一个字符或子串的位置;若查找失败,返回npos,即-1(打印出来为4294967295)。

  • find()函数

//find() 函数用于在 string 字符串中查找子字符串出现的位置,它其中的两种原型为:
size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char c, size_t pos = 0) const;
/*第一个参数为待查找的子字符串(或者字符),它可以是 string 字符串,也可以是C风格的字符串。
第二个参数为开始查找的位置(下标);如果不指明,则从第0个字符开始查找。
*/

/*find() 函数最终返回的是子字符串第一次出现在字符串中的起始下标。
如果没有查找到子字符串,那么会返回一个无穷大值 4294967295。
*/
string s1("Houhailin");
string s2("hailin");
int p1=s1.find('h');//p1=3,从s1字符串的第0个字符开始查找字符h
int p2=s1.find('i',6);//p2=7,从s1字符串的第6个字符开始查找字符i
int p3=s1.find(s2);//p3=3,从s1字符串的第0个字符开始查找字符串s2
if(s1.find('n')==string::npos)//如果没有找到,返回npos
{
   cout<<"not find"<<endl;
}


/*
从第pos个字符开始,查找字符串s前n个字符
若n大于s的长度,则返回nops
*/
size_t find (const char* s, size_t pos, size_t n) const;
string str("abcghasc");
int w1=str.find("abchj",0,3);//相当于str.find("abc",0);
int w2=str.find("abchj",0,5);//相当于str.find("abchj",0);
int w3=str.find("abchj",0,6);//返回nops
  • rfind() 函数

rfind()和find()用法相似,就是倒着查找,后面的数字代表着就是从第几个开始查找,直到串首)。

  • find_first_of()

//在源串中从位置pos起往后查找,只要在源串中遇到一个字符,该字符与目标串中任意一个字符相同,
就停止查找,返回该字符在源串中的位置;若匹配失败,返回npos
size_t find_first_of (const string& str, size_t pos = 0) const noexcept;
size_t find_first_of (const char* s, size_t pos = 0) const;
size_t find_first_of (const char* s, size_t pos, size_type n) const;
size_t find_first_of (char c, size_t pos = 0) const noexcept;
  • find_last_of()

该函数与find_first_of()函数相似,只不过查找顺序是从指定位置向前。

  • find_first_not_of()

/*
   在源串中从位置pos开始往后查找,只要在源串遇到一个字符,该字符与目标串中的任意一个字符
都不相同,就停止查找,返回该字符在源串中的位置;若遍历完整个源串,都找不到满足条件的字符,
则返回npos。
*/
size_t find_first_not_of (const string& str, size_t pos = 0) const noexcept;
size_t find_first_not_of (const char* s, size_t pos = 0) const;
size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
size_t find_first_not_of (char c, size_t pos = 0) const noexcept;
  • find_last_not of()

find_last_not_of()与find_first_not_of()相似,只不过查找顺序是从指定位置向前。

特性描述

  1. 使用str.size()或str.length()来获取字符串长度

string str("hello!");
int len1 = str.size();
int len2 = str.length();
  1. bool empty();//判断字符串是否为空,空的话返回true,否则返回false

string str2;
if (str2.empty())
 {
     cout << "str2 is empty." << endl;
 }
  1. int max_size(); //返回string对象中可存放的最大字符串的长度

  1. int capacity(); //返回当前容量(即string中不必增加内存即可存放的元素个数)

  1. void resize(int len,char c); //把字符串当前大小置为len,并用字符c填充不足的部分

字符串比较

  1. int compare(string &s) ;//比较当前字符串和s的大小

// 比较字符串>时返回1,<时返回-1,==时返回0 
string str1="fjksdfjnf";
string str2="sjkafkjjks";
if (0 == str1.compare(str2))
  {
        cout << "str1 和 str2 相等" << endl;
  }
  1. int compare(int pos, int n,string &s); //比较当前字符串从pos开始的n个字符组成的字符串与s的大小

  1. int compare(int pos, int n,string &s,int pos2,int n2);//比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小

  1. int compare(char *s) ;//比较当前字符串和s所指向的字符串的大小

  1. int compare(int pos, int n,char *s) ;//比较当前字符串从pos开始的n个字符组成的字符串与s所指向的字符串的大小

  1. int compare(int pos, int n,char *s, int pos2) ;//比较当前字符串从pos开始的n个字符组成的字符串与s所指向的从pos2开始到末尾的字符串大小

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值