C++字符串string类常用操作详解【初始化、遍历、连接】 std::replace

任务要将string字符串里面的每个'|',转换为'\n'

第一反应就是遍历string,然后替换。

转自C++ string的遍历_涛歌依旧的博客-CSDN博客_c++遍历字符串

#include <iostream>
#include <iostream>
#include <string>
using namespace std;
 
int main()
{
        string s = "123,456,789,";
        unsigned int iSize = s.size();
        cout << iSize << endl;   // 12
        cout << s.length() << endl;   // 12
 
        unsigned int i = 0;
        int iCount = 0;
        for(i = 0; i < iSize; i++)
        {
                if(',' == s[i])
                {
                        iCount++;
                }
        }
 
        cout << iCount << endl; // 3
 
    return 0;
}

这是数组替换,因为是c++,所有想用迭代器遍历,于是找了下面这个文章

分隔符之前转自:C++字符串string类常用操作详解(一)【初始化、遍历、连接】 - 邱明成 - 博客园

代码示例:

#include <iostream>  
#include "string"  
  
using namespace std;  
  
//字符串初始化  
void strInit()  
{  
    cout << "字符串初始化:"  <<endl;  
  
    string s1 = "abcdefg";  //初始化方式1  
    string s2("abcdefg");   //初始化方式2  
    string s3 = s2;         //通过拷贝构造函数 初始化s3  
    string s4(7,'s');       //初始化7个s的字符串  
  
    cout << "s1 = "<< s1 << endl;  
    cout << "s2 = "<< s2 << endl;  
    cout << "s3 = "<< s3 << endl;  
    cout << "s4 = "<< s4 << endl;  
}  
  
//字符串遍历  
void strErgo()  
{  
    cout << "字符串遍历:"  <<endl;  
  
    string s1 = "abcdefg";  //初始化字符串  
      
    //通过数组方式遍历  
    cout << "1、通过数组方式遍历:"  <<endl;  
    for (int i = 0; i < s1.length(); i++)  
    {  
        cout << s1[i] << " ";  
    }  
    cout << endl;  
  
    //通过迭代器遍历  
    cout << "2、通过迭代器遍历:"  <<endl;  
    for(string::iterator it = s1.begin(); it!= s1.end(); it++)  
    {  
        cout << *it << " ";  
    }  
    cout << endl;  
  
    //通过at()方式遍历  
    cout << "3、通过at()方式遍历:"  <<endl;  
    for (int i = 0; i < s1.length(); i++)  
    {  
        cout << s1.at(i) << " ";        //此方式可以在越界时抛出异常  
    }  
    cout << endl;  
}  
  
//字符指针和字符串的转换  
void strConvert()  
{  
    cout << "字符指针和字符串的转换:"  <<endl;  
    string s1 = "abcdefg";  //初始化字符串  
  
    cout << "string转换为char*:"  <<endl;  
    //string转换为const char*  
    cout << s1.c_str() <<endl;  //s1.c_str()即为s1的const char *形式  
  
    cout << "char*获取string内容:"  <<endl;  
    //char*获取string内容  
    char buf[64] = {0};  
    s1.copy(buf, 7);//复制7个元素  
    cout << buf <<endl;  
}  
  
//字符串连接  
void strAdd()  
{  
    cout << "字符串连接:"  <<endl;  
  
    cout << "方式1:"  <<endl;  
    string s1 = "123";  
    string s2 = "456";  
    s1 += s2;  
    cout << "s1 = "<< s1 << endl;  
  
    cout << "方式2:"  <<endl;  
    string s3 = "123";  
    string s4 = "456";  
    s3.append(s4);  
    cout << "s3 = "<< s3 << endl;  
}  
int main()  
{  
    //初始化  
    strInit();  
    cout << endl;  
    //遍历  
    strErgo();  
    cout << endl;  
    //字符指针类型和字符串转换  
    strConvert();  
    cout << endl;  
    //字符串连接  
    strAdd();  
    cout << endl;  
    system("pause");  
    return 0;  
}  

程序运行结果:

字符串初始化:  
s1 = abcdefg  
s2 = abcdefg  
s3 = abcdefg  
s4 = sssssss  
  
字符串遍历:  
1、通过数组方式遍历:  
a b c d e f g  
2、通过迭代器遍历:  
a b c d e f g  
3、通过at()方式遍历:  
a b c d e f g  
  
字符指针和字符串的转换:  
string转换为char*:  
abcdefg  
char*获取string内容:  
abcdefg  
  
字符串连接:  
方式1:  
s1 = 123456  
方式2:  
s3 = 123456  
  
请按任意键继续. . .  

-----------------------------------------------------分隔符-------------------------------------------------------------------

导师:有一个叫做std::replace,知道它就更简单了

std::string text = “1|11|111|1111”;
std::replace(text.begin(),text.end(),'|','\n');//将'|'改为'\n'

over,结束,又被导师的风姿折服

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值