正在加载……
专注、离线、切勿分心
string类的一些public成员函数,通过这些函数可以方便地对字符串进行赋值、清空,实现不同字符串间的比较,字符的插入、删除、追加,以及搜索查找

C++的 string 相当于 const char *

提取字串substr()
string类提供了substr成员函数用于提取子串,以string str(“98765432112345678”)为例:

(1)str.substr();        //返回str的全部内容
(2)str.substr(9);       //截断前九个字符,即“12345678”
(3)str.substr(5,6);     //从下标为5的字符开始往后截取,一共截取6个字符,即“432112”

搜索字串出现的位置find() :
string s1="Welcome to C++ World";   //计算机默认从0开始
int size=s1.find("o");                    //size=4 , 查找o在字符串中首次出现的位置
int size1=s1.find_last_of("o");           //size1=16 , 查找o在字符串中最后出现的位置
int size2=s1.find_first_of("abc");        //size2=3,查找第一次出现了字符串“abc”中元素的位置
int size3=s1.find_last_of("abcd");        //size3=19 , 查找最后一次出现了字符串“abcd”中元素的位置
int szie4=s1.find_first_not_of("abc");    //size4=1 , 查找第一位不位于字符串“abc”中元素的元素的位置
int szie5=s1.find_last_not_of("abc");     //size5=19 , 查找第一位不位于字符串“abc”中元素的元素最后一次所在的位置

#include <iostream>
#include<string>
using namespace std;
int main()
{
        string s1="hello";
        string s2=",world";
        string s3=s1+s2+"~~~~";
        cout<<s3<<endl;
        cout<<"sizeof(s3)="<<sizeof(s3)<<endl;  //所有string对象的空间大小都是8,和它具体是什么没关系
        cout<<"s3.size()="<<s3.size()<<endl;    //计算长度的方法
        string s4="hello,world";
        string s5=s4.substr(0,5);
        cout<<"s5="<<s5<<endl;
        string s6=s4.substr(s4.find("w"),5);
        cout<<"s6="<<s6<<endl;
        for(int i=0;i<s4.size();i++)
        {
                cout<<s4[i];
        }
        cout<<endl;

        string::iterator it=s4.begin();       //利用迭代器输出每个字符    begin-->end 是一个左闭右开的区间[)
        for(;it!=s4.end();++it)
        {
                cout<<*it<<endl;
        }
        cout<<endl;
        return 0;
}


strstr()
    char *strstr(const char *haystack, const char *needle);
   第一个参数是原字符串,第二个参数是要查找的字串,返回字串在原串中的首地址
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
        char str[]="hello,world";
        char *p=new char[strlen("world")+1];
        char *p1=strstr(str,"world");
        strcpy(p,p1);
        cout<<p<<endl;
        return 0;
}



 istream &getline( char *buffer, streamsize num );
 istream &getline( char *buffer, streamsize num, char delim );

getline()函数用于输入流,读取字符到buffer中,直到下列情况发生:

  • num - 1个字符已经读入,
  • 碰到一个换行标志,
  • 碰到一个EOF,
  • 或者,任意地读入,直到读到字符delimdelim字符不会被放入buffer
#include<string>       //模板类
using namespace std;
string s;
getline(cin,s);   //cin获取一行,以  \n  表示结束
#include <iostream>
#include<stdlib.h>
#include<string>
using namespace std;
int main()
{
        char *p=(char*)malloc(sizeof(char)*100);
        cin.getline(p,10);
        cout<<p<<endl;
        return 0;
}
#include <iostream>
#include<string>
using namespace std;
int main()
{
        string s;
        while(getline(cin,s))  //不为EOF(ctrl+z)
        cout<<s<<endl;
        return 0;
}

    

#include <iostream>
#include<string>
using namespace std;
int main()
{
        char* p=new char[100];
        //cout << cin.good() << endl;      //查看当前输入流是不是正常(1)
        cin.getline(p,10);                 //输入流长度12,这里只读取10个,输入流会异常
        cout<<"p:"<<p<<endl;
        //cout << cin.good() << endl;      //前面异常,这里(0)
        string s;
        while(getline(cin,s))              //前面异常,这里就不会进入while循环,要向进入循环只能前面大小改为12以上
        cout<<s<<endl;
        return 0;
}

         

//把string类型字符串转成char * 类型
string filename;
char name[100];  
strcpy(name,filename.c_str());
//string类型的字符串可以直接比较相不相同
string s1="hello";
string s2="world";
if(s1==s2);


posted on 2018-04-04 08:55  正在加载……  阅读(193)  评论(0编辑  收藏  举报