码迷,mamicode.com
首页 > 编程语言 > 详细

C++(十)— 字符串进行插入、替换、删除操作

时间:2018-08-05 21:26:30      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:指定位置   length   int   ios   ase   font   pre   ras   删除   

 1、C++中对字符串进行插入、替换、删除操作

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include <vector>
#include<string>
using namespace std;

int main()
{
    string s = "123456";

    // 在字符串指定位置前插入字符,
    cout << s.insert(2, "abc") << endl;// 输出 :12ab3456

    // 插入指定字符串的前n个字符
    cout << s.insert(2, "abc", 2) << endl;// 12ab3456

    //替换指定索引开始的指定长度的子串
    cout << s.replace(2, 2, "abc") << endl;// 12abc56,替换“34”为“abc”

    //删除指定索引开始的指定长度的字符
    string word = "34";
    size_t index = s.find(word);
    if (index != string::npos)
        cout << s.erase(index, word.length()) << endl;//1256,删除了"34"

    return 0;
}

 

C++(十)— 字符串进行插入、替换、删除操作

标签:指定位置   length   int   ios   ase   font   pre   ras   删除   

原文地址:https://www.cnblogs.com/eilearn/p/9427027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!