最实用的的c++中string函数的用法,没有之一。

纯属原创,

                                                             String函数的用法                                                          

string的定义及初始

string s1 = "hello"; //初始化字符串

string s2 ("world"); //另一种初始化

string s3;   //初始化字符串,空字符串

string s4(5, 'a'); //s4由连续5个a组成,即s4="aaaaa";

string s5(s1,2,3); //从s1的2位置的字符开始,连续3个字符赋值给s5,即s5="llo";

string s6(s1, 1); //从s1的2位置的字符开始,将后续的所有字符赋值给s6,即s6="ello";

s1 = "s" + s2 + "s" + "s"; //正确,“+”的两边要保证至少有一个string类型string+“s”

Char类型数组转化成String类型

假设c字符串定义为char ch[]="hello world!";

1.向构造函数传入c字符串创建string对象:

string str(ch);

2.使用拷贝构造函数创建string对象:

string str = ch;

3.对已有的string对象调用string类内部定义的赋值运算符:

string str;

str = ch;

前两种类似,但和第三种有较大区别,前两种是运用构造函数直接创建一个内容与c字符串一致的string对象;第三种是c++标准库编写的string类的内部重载了赋值运算符,使之能够以c字符串作为右操作数对string对象进行赋值,使string对象的内容与c字符串一致。

 

字符串截取

用法1:截取下标从2(第3个字符)开始到字符串结尾的字符串

string str = "ABCDEFG";

string cut= str.substr(2);

最终,cut="CDEFG"。

衍生如果str="image007.jpg",而我们想知道其文件扩展名(filename extension),那么可以这样操作:

string str = "image007.jpg";

string cut= str.substr(str.find_last_of(".")+1);

最终,cut="jpg",得到扩展名。其中,str.find_last_of(".")返回str字符串中最后一个'.'的所在下标,这里返回8(int)。

用法2:截取下标从2(第3个字符)开始截取3个字符的字符串

string str = "ABCDEFG";

string cut= str.substr(2,3)

最终,cut="CDE",即从下标为2开始向后数3位。

衍生:如果str="image007.jpg",而我们只要其文件名而不要扩展名,那么可以这样操作:

string str = "image007.jpg";

string cut= str.substr(0,str.find_last_of("."));

最终,cut="image007",得到不含扩展名的文件名。

 

String 中的查找

string str1 = "cup,car,person,car,booo";

string str2 = "ako";

int num_1 = str1.find_first_of(str2);//返回str1中第一个与str2的第一个字符('a')相同字符的下标 ,返回5

int num_2 = str1.find_first_not_of(str2);//返回str1中第一个与str2的第一个字符('a')不同字符的下标 ,返回0

int num_3 = str1.find_last_of(str2);//返回str1中最后一个与str2的最后一个字符('o')相同字符的下标 ,返回22

int num_4 = str1.find_last_not_of(str2);//返回str1中最后一个与str2的最后一个字符('o')不同字符的下标 ,返回19

详细:

s = "asdfg";

 n = s.find("asdfg");  //n=0 查找 全匹配   

    n=s.find(100);        //n=2,将100转为字符查找,d的ascii码为100   

    n=s.find('dfg');      //n=4    

    n = s.find("dfg");    //n=2    

    n=s.find("dhh");      //n=string::npos=4294967295

    n = s.find("wer");    //n=string::npos=4294967295

    n=s.find('f');        //n=3

    n = s.find("f");      //n=3

    char as = 'dfg';      //as='g'

 

    n = s.find_first_of("asdfg");  //n=0,查找包含子串中的任何字符, 返回第一个位置   

    n = s.find_first_of(3);        //n=string::npos=4294967295   

    n = s.find_first_of('dfg');    //n=4  

    n = s.find_first_of("dfg");    //n=2

    n = s.find_first_of("dhh");    //n=2  

    n = s.find_first_of("wer");    //n=string::npos=4294967295

    n = s.find_first_of('f');      //n=3,从pos开始查找字符第一次出现的位置

    n = s.find_first_of("f");      //n=3

 

    n = s.find_last_of("asdfg"); //n=4,查找包含子串中的任何字符,返回最后一个位置

    n = s.find_last_of(3);         //n=string::npos=4294967295   

    n = s.find_last_of('dfg');     //n=4

    n = s.find_last_of("dfg");     //n=4

    n = s.find_last_of("dhh");     //n=2

    n = s.find_last_of("wer");     //n=string::npos=4294967295

    n = s.find_last_of('f');       //n=3

    n = s.find_last_of("f");       //n=3

 

    n = s.find_first_not_of("asdfg");//n=string::npos=4294967295,查找不包含子串中的任何字符,返回第一个位置。从当前串中查找第一个不在参数串中的字符出现的位置

    n=s.find_first_not_of(3);      //n=0  

    n=s.find_first_not_of('dfg');  //n=0  

    n = s.find_first_not_of("dfg");//n=0

    n=s.find_first_not_of("dhh");  //n=0

    n = s.find_first_not_of("wer");//n=0

    n=s.find_first_not_of('f');    //n=0

    n = s.find_first_not_of("f");  //n=0

 

    n = s.find_last_not_of("asdfg");//n=string::npos=4294967295,查找不包含子串中的任何字符,返回最后一个位置

    n=s.find_last_not_of(3);          //n=4  

    n=s.find_last_not_of('dfg');      //n=3

    n = s.find_last_not_of("dfg");    //n=1

    n=s.find_last_not_of("dhh");      //n=4

    n = s.find_last_not_of("wer");    //n=4

    n=s.find_last_not_of('f');        //n=4

    n = s.find_last_not_of("f");      //n=4

 

注:找到就返回第一次出现的串的第一个字符位置,找不到返回-1。

1.find()用法(查找指定字串是否存在)string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos。(返回值可以看成是一个int型的数)

#include<cstring>

#include<cstdio>

#include<iostream>

using namespace std;

int main()

{

    ////find函数返回类型 size_type

    string s("1a2b3c4d5e6f7jkg8h9i1a2b3c4d5e6f7g8ha9i");

    string flag;

    string::size_type position;

    //find 函数 返回jk 在s 中的下标位置

    position = s.find("jk");

    if (position != s.npos)  //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295,

    {

        printf("position is : %d\n" ,position);

    }

    else

    {

        printf("Not found the flag\n");

    }

}

2.查找某一给定位置后的子串的位置

  //从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标

     position=s.find("b",5);

  1. 查找所有子串在母串中出现的位置

//查找s 中flag 出现的所有位置。

    flag="a";

    position=0;

    int i=1;

    while((position=s.find(flag,position))!=string::npos)

    {

        cout<<"position  "<<i<<" : "<<position<<endl;

  1. Rfind()用法,反向查找子串在母串中出现的位置,通常我们可以这样来使用,当正向查找与反向查找得到的位置不相同说明子串不唯一。

flag="jkll";

position=s.rfind (flag);

printf("s.rfind (flag) :%d\n",position);

反向查找所有的

flag="jk";

    position=s.length()-1;

    int i=1;

    while((position=s.rfind(flag,position))!=string::npos)

    {

        cout<<"position  "<<i<<" : "<<position<<endl;

        position--;

        i--;

}

String中的插入

#include <iostream>

#include <string>

#include<cstring>

using namespace std;

 

int main (){

  string str="to be question";

  string str2="the ";

  string str3="or not to be";

  string::iterator it;

  // used in the same order as described above:

  str.insert(6,str2);                 // to be (the )question

  str.insert(6,str3,3,4);             // to be (not )the question

  str.insert(10,"that is cool",8);    // to be not (that is )the question

  str.insert(10,"to be ");            // to be not (to be )that is the question

  str.insert(15,1,':');               //加一个'.' to be not to be(:) that is the question

  it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question

  str.insert (str.end(),3,'.');       // to be, not to be: that is the question(...)

  str.insert (it+2,str3.begin(),str3.begin()+3); // (or )*/

  cout << str << '\n';

  return 0;

}

String中的删除

  1. string.erase(pos,n)          //删除从pos开始的n个字符    string.erase(0,1);   删除第一个字符

string s;

s.erase(1,2);

 

  1. string.erase(pos)           //删除pos处的一个字符(pos是string类型的迭代器)

i = s.begin()+3;

s.erase(i);

 

3、string.erase(first,last)    //删除从first到last中间的字符(first和last都是string类型的迭代器)

s.erase(s.begin()+1,s.end()-1);

 

猜你喜欢

转载自blog.csdn.net/qq_40859951/article/details/82765877