C++中 string 中的常用方法使用心得分享!

string 字符串在所有的语言中都非常重要,c++也不例外,接下来我们将介绍string中的常用方法

1. size() 和 length() 函数 : 他们返回字符串的真实长度,且不会因为空格而截断,这两个方法完全等价,使用及输出如下:

  #include<iostream>  #include<string>  using namespace std;     int main(void)  {    string s = "dasddasd";    printf("size()返回的长度为:%lunlength()返回的长度为:%lu",s.size(),s.length());    return 0;  }

C++中 string 中的常用方法使用心得

2. find()函数和rfind()函数 : 这两个函数用于查找字串在母串中的位置,并且返回该位置,当然如果找不到就会返回一个特别的标记string::nops,而find()函数是从字符串开始指针向后进行查找,rfind()函数是从字符串的结束指针开始向前查找,其使用及输出如下:

  #include<iostream>  #include<string>  using namespace std;    int main(void)  {    string s = "hello worldh";    int index = s.find("h");   // 从串首向后查找    int index2 = s.find("h",2)  // 固定位置后子串在母串的位置    int index1 = s.rfind("h"); // 从串尾向前查找    printf("(find()):字母h在母串中的位置为:%dn", index);     printf("(rfind()):字母h在母串中的位置为:%d", index1);    return 0;  }

C++中 string 中的常用方法使用心得

值得注意的是我们可以通过组合使用这两个函数来实现判断该子串是否唯一存在于母串中,其实现代码如下:

  #include<iostream>  #include<string>  using namespace std;    inline bool whetherOnly(string &str,string &base){    return base.find(str) == base.rfind(str);   }

3. find_last_of()函数和find_first_of()函数:从函数名我们也可以知道find_last_of()函数是找这个子串在母串中最后一次出现的位置并且将该位置返回;而find_first_of()函数是找这个子串在母串中最后一次出现的位置并将该位置返回,其使用及输出如下:

  #include <iostream>  #include <string>  using namespace std;    int main(void)  {    string s = "hello worldh";       int index = s.find_first_of("h");    int index1 = s.find_last_of("h");    printf("(find_first_of()):字母h在母串中的位置为:%dn", index);    printf("(find_last_of()):字母h在母串中的位置为:%d", index1);  }

C++中 string 中的常用方法使用心得

4.assign()函数:该函数用于将目标串的值复制到该串上,并且只复制值,其使用及输出如下:

  #include <iostream>  #include <string>  using namespace std;    int main(void)  {    string s = "hello worldh";    s.clear();    s.assign("hello world");    cout<<s<<endl;      }

C++中 string 中的常用方法使用心得

5.clear()函数,把当前字符串清空,这时候如果调用string::size()函数或string::length()函数将返回0,其使用及输出如下:

  #include <iostream>  #include <string>  using namespace std;    int main(void)  {    string s = "hello worldh";    s.clear();    cout<<"clear后的串的长度"<<s.size()<<endl;  }

C++中 string 中的常用方法使用心得

6.resize()函数,该函数可以将字符串变长到指定长度,若小于原本字符串的长度,则会截断原字符串;这个函数的一个重载形式是str.resize(length,'s') 可以用该输入字符’s’来对字符串进行扩充至length的长度,该函数的使用及输出如下:

  #include <iostream>  #include <string>  using namespace std;    int main(void)  {    string s = "hello worldh";    s.resize(5);    // s会变为 hello    cout<<s<<endl;    s.resize(10,'C'); // s 会变为 helloCCCCC    cout<<s<<endl;      }

C++中 string 中的常用方法使用心得

7. replace(pos,len,dist)函数: 该函数用于将该串从pos位置开始将长度为len的字串替换为dist串,值得注意的是该函数只替换一次,这与市面上的py和java等语言不一样,需要留意,该函数的使用和输出如下:

  #include <iostream>  #include <string>  using namespace std;    int main(void)  {    string s = "hello worldh";    s.replace(s.find("h"),2,"#"); // 把从第一个h开始的两个字符变为一个字符 #    cout<<"替换后的字符串为: "<<s<<endl;      }

C++中 string 中的常用方法使用心得

那么既然C++本身不提供,替换所有子串的函数,我们就自己实现一个,其代码如下:

  // 替换字符串里的所有指定字符  string replace(string &base, string src, string dst) //base为原字符串,src为被替换的子串,dst为新的子串  {    int pos = 0, srclen = src.size(), dstlen = dst.size();    while ((pos = base.find(src, pos)) != string::npos)     {      base.replace(pos, srclen, dst);      pos += dstlen;    }    return base;  }

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/484660.html

(0)
上一篇 2020年11月10日
下一篇 2020年11月10日

精彩推荐