c++的string使用

string 类提供字符串处理函数,利用这些函数,程序员可以在字符串内查找字符,
提取连续字符序列(称为子串),以及在字符串中删除和添加。我们将介绍一些主要函数:
1、函数find_first_of()和 find_last_of() 执行简单的模式匹配
例如:在字符串中查找单个字符c。
函数find_first_of() 查找在字符串中第1个出现的字符c,而函数find_last_of()查找最后
一个出现的c。匹配的位置是返回值。如果没有匹配发生,则函数返回-1.
int find_first_of(char c, int start = 0):
查找字符串中第1个出现的c,由位置start开始。
如果有匹配,则返回匹配位置;否则,返回-1.默认情况下,start为0,函数搜索
整个字符串。
int find_last_of(char c):
查找字符串中最后一个出现的c。有匹配,则返回匹配位置;否则返回-1.
该搜索在字符末尾查找匹配,所以没有提供起始位置。
代码:
string str = “Mississippi”;
int index;
// 's ’ 在index 为 2、3、5、6处出现
index = str.find_first_of(‘s’,0); // index为 2
index = str.find_first_of(‘s’,4); // index为 5
index = str.find_first_of(‘s’,7); // index为 -1

 // ‘s’的最后出现在 index= 6  
 index = str.find_last_of('s');  
 // while 循环输出每个'i'的index  
 while((index = str.find_first_of('i', index))!= -1)  
 {  
    cout << "index" << index << " ";  
    index++;   // restart search at next indx  
 }

2、字符串中提取连续字符序列,既子串。
这个操作假定位置 start 和 字符数 count。
string substr(int start=0,int count= -1);
从起始位置开始复制字符串中的count 个字符,并返回这些字符作为子串。
如果字符串尾部小于count字符或者count 为-1,则字符串尾停止复制。
如果不使用参数调用只包括位置start,则substr()返回从位置开始到字符串尾部的子串。

3、find()函数在字符串中查找指定模式。该函数将字符串s和位置start作为参数,并查找
s的匹配作为子串。
int find(const string& s,int start = 0):
该搜索获得字符串s和位置start,并查找s的匹配作为子串。
如果有匹配,则返回匹配的位置;否则返回-1。默认情况下,
start为0,函数搜索整个字符串。
代码:
string fullname = “Mark Tompkin”, firstname, lastname;
int index;

index = str.find_last_of(' ');   // index is 4  
// firstname = "Mark" lastname = "Tompkin"  
firstname = fullname.sub string(0,index);  
lastname = fullname.substring(index+1);  
 
index = fullname.find("kin");         // 在 index = 9 匹配 "Kin"  
index = fullname.find("omp",0);    // 在 index = 6 匹配 "omp"  
index = fullname.find("omp",7);    // index is -1 (无匹配)

4、添加和删除字符串
字符连接(+、+=)是在字符串尾添加字符串。insert()函数扩展了这个能力,
允许在任意位置添加字符串。为了从字符串。为了从字符串中删除字符串,
函数erase()可以从指定的位置开始删除字符。

void insert(int statr,const string& s):
           将子串s放入字符串中,起始于位置start。插入操作增加了原始字符串的长度。
 
 void erase(int start=0,int count=-1):
            从start开始,从字符串中删除count个字符。如果现有的字符串少于count个
 字符,或者count为-1,则删除到字符串尾部的所有字符。默认情况下,start为0,函数
 从字符串是起始位置开始删除字符串。默认情况下,函数也删除到字符串尾。
 需要注意的是,不使用参数调用erase()函数时,将把字符串截断为长度为0的空字符串

代码:
string str = “endfile”;
string s = “string object type”;
str += " mark";
str.inset(3, “-of-”); // str 是 “end-of-file mark”
s.erase(7,7); // s 是 “string type”
// 从index 为3处删除4个字符
s.erase(3,4);
cout << s; // 输出:“strtype”

5、c_str()返回c语言风格字符串的地址。
将字符串对象转换为c语言风格字符串。
char c_str();
返回一个等价于字符串对象的c语言风格字符串的地址。返回类型char
表示c
语言风格字符串第1个字符的地址

6、// 识别最后一个’/‘的位置。注意:由于
// 转义码如’/n’以/起始,
// c++ 使用’//'表示 /

7、isdigits(char ch)判断是否是数字

#include<iostream>
#include"string"
#include"algorithm"
using namespace std;

//string的赋值
void f1()
{
    string s1 ="shihao";        //string是一个类
    string s2 ("bbbbbb");
    string s3 = s1;
    string s4(10, 'a');     //等价于string s4 ="aaaaaaaaaa";
    cout << s1 << endl;
    cout << s2 << endl;
    cout << s3 << endl;
    cout << s4 << endl;
}

//string的遍历
void f2()
{
    string s1 = "abcdefg";

    //1:数组方式
    for(int i = 0;  i < s1.length(); i++)
    {
        cout << s1[i] << " ";
    }
    cout << endl;

    //2:迭代器
    for(string::iterator it = s1.begin(); it != s1.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    //3  异常处理
    try
    {
        for(int i = 0; i < s1.length() + 3; i++)
        {
            cout << s1.at(i) << " ";   //当出现错误时, 会向外抛出异常
        }
    }
    catch(...)
    {
        cout << "发生异常" << endl;
    }

    try
    {
        for(int i = 0;  i < s1.length() + 3; i++)   //出现错误, 不向外抛出异常
        {
            cout << s1[i] << " ";
        }
    }
    catch(...)
    {
        cout << "发生异常" << endl;
    }
}

//字符指针和sting的转换
void f4()
{
    //1:
    string s1 = "aaabbb";
    cout << s1.c_str() << endl;

    //2:
    char buf[128] = {0};
    s1.copy(buf, 3, 1);    //拷贝3个字符,从1个字符开始(位置下标从0开始)    //注:不会自动加上字符串结束标志
    cout << "buf: " << buf << endl;
}

//字符串的连接
void f5()
{
    //1:
    string s1 = "aaa";
    string s2 = "bbb";
    s1 = s1 + s2;
    cout << "s1: " << s1 << endl;

    //2:
    string s3 = "333";
    string s4 = "444";
    s3.append(s4);
    cout << "s3: " << s3 << endl;
}

//字符串的查找和替换
void f6()
{
    string s1 = "wbm hello wbm 111 wbm 222 wbm 333";

    //查找 从查找位置开始第一个出现的下标
    int index = s1.find("wbm", 1);  //位置下标从0开始
    cout <<"index: " << index << endl << endl;

    //查找每一次wbm出现的下标
    int offindex = s1.find("wbm", 0);
    while (offindex != string::npos)   //不等于-1
    {
        cout << "offindex: " << offindex << endl;
        offindex++;
        offindex = s1.find("wbm", offindex);
    }

    //把所有的wbm换成大写
    offindex = s1.find("wbm", 0);
    while (offindex != string::npos)   //不等于-1
    {
        s1.replace(offindex, 3, "WBM");
        offindex++;
        offindex = s1.find("wbm", offindex);
    }
    cout << endl << "s1替换后的结果为: " << s1 << endl;

    //把aaa替换成大写
    string s2 = "aaa bbb ccc";
    s2.replace(0, 3, "AAA");    //从第0个位置开始替换3个
    cout << endl << "s2: " << s2 << endl;
}

//区间删除和插入
void f7()
{
    string s1 = "hello1 hello2 hello1";
    string::iterator it = find(s1.begin(), s1.end(), 'l');
    if (it != s1.end())
    {
        s1.erase(it);   //删除
    }
    cout << "s1删除l以后的结果为:" << s1 << endl;

    s1.erase(s1.begin(), s1.end());
    cout << "s1全部删除:" << s1 << endl;
    cout << "s1的长度为: " << s1.length() << endl;

    //插入
    string s2 = "BBB";
    s2.insert(0, "AAA");
    cout << "s2: " << s2 << endl;
}

//大小写转换
void f8()
{
    string s1 = "AAAbbb";
    transform(s1.begin(), s1.end(), s1.begin(), ::tolower);       //transform(first,last,result,op);
    cout << "s1全部转化为小写为: " << s1 << endl;
    transform(s1.begin(), s1.end(), s1.begin(), ::toupper);       //transform(first,last,result,op);
    cout << "s1全部转化为大写为: " << s1 << endl;
}

int main()
{
    f8();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值