1. 程式人生 > >C++ STL string字串替換 replace函式的使用

C++ STL string字串替換 replace函式的使用

//C++ string字串替換
//replace()函式的使用
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string var("abcdefghijklmnopqrst");
    const string dest("1234");
    string dest2("567891234");
    var.replace(3,3,dest);
    cout<<"1: "<<var<<endl;
    var="abcdefghijklmnopqrst";
    var.replace(3,1,dest.c_str(),1,3);
    cout<<"2: "<<var<<endl;
    var="abcdefghijklmnopqrst";
    var.replace(3,1,5,'X');
    cout<<"3: "<<var<<endl;
    string::iterator itA,itB;
    string::iterator itC,itD;
    itA=var.begin();
    itB=var.end();
    var="abcdefghijklmnopqrst";
    var.replace(itA,itB,dest);
    cout<<"4: "<<var<<endl;
    itA=var.begin();
    itB=var.end();
    itC=dest2.begin()+1;
    itD=dest2.end();
    var="abcdefghijklmnopqrst";
    var.replace(itA,itB,itC,itD);
    cout<<"5: "<<var<<endl;
    var="abcdefghijklmnopqrst";
    //這種方式會限定字串替換的最大長度
    var.replace(3,1,dest.c_str(),4);
    cout<<"6: "<<var<<endl;
    return 0;
}

相關推薦

C++ STL string字串替換 replace函式的使用

//C++ string字串替換 //replace()函式的使用 #include <iostream> #include <string> using namespace s

[C++/STL] string字串關於copy函式的C4996錯誤

今天晚上,在使用Visual Stdio 2013使用string類的copy函式時,編譯出現以下錯誤: error C4996: 'std::basic_string<char,std::char_traits<char>,std::allocator&

String字串replace函式和正則表示式

package zhengze; public class sss { public static void main(String[] args) { // TODO Auto-generated method stub          String str="

C++ STL string的建構函式

首先是弄清下面的概念(下面這段是轉載的):<string.h><string.h>是C版本的標頭檔案,包含比如strcpy、strcat之類的字串處理函式。<cstring

C++ stl---string類裡常用的成員函式

string類的建構函式: string(const char *s);    //用c字串s初始化string(int n,char c);     //用n個字元c初始化 此外,string類還支援預設建構函式和複製建構函式,如string s1;string s2="hello";都是正確的寫法。當

c語言】字串替換空格:請實現一個函式,把字串中的每個空格替換成“%20”

// 字串替換空格:請實現一個函式,把字串中的每個空格替換成“%20”。 // 例如輸入“we are happy.”,則輸出“we%20are%20happy.” #include <stdio.h> #include <assert.h>

淺學C++ STL--string容器程式碼筆記

 capacity()  string類capacity()容器的大小會因編譯器的不同而得到的容量大小有所不同; VS2017中string類首次分配空間為15,溢位之後分配為+16,之後每溢位16就+16,簡而言之規律就是:15+16+16+16+; VC++6.0

C語言的字串輸入fgets()函式

C語言的字串輸入fgets()函式 圖片來源-百度圖片 fgets()函式簡介 讀字串函式fgets()的功能是從指定的檔案中讀一個字串到字元陣列中,函式呼叫的形式為: fgets(字元陣列名,n,檔案指標),要從鍵盤輸入時檔

JAVA字串替換replace、replaceAll、replaceFirst之間區別詳解

String的replaceAll跟replaceFirst用到了正則表示式 String s = "my.test.txt"; System.out.println(s.replace(".", "#")); System.out.println(s.replaceAll

C 實現 刪除字串空白符的函式 strtrim

說在前面的話 字串操作在很多C語言比賽裡面都有涉及,最近公眾號裡面的C語言比賽,都是兩個關於字串操作的題目,希望大家認真看題目。 直接上程式碼 /*****************************************************************

C語言:字串處理類函式

strlen() 求字串的實際長度(不包括'\0')   strcpy() char *strcpy(char* dest, const char *src); strcpy(目標串,源串); strcp

VS程式設計,C#中string字串過長,回車換行寫在不同的行的一種方法。

1、使用@對字串進行轉義。 “”雙引號裡裡面的特殊字元不再具有轉義功能,例如\n不再被轉義成換行符。 2、使用@對字串進行轉義,若字串中包含雙引號,則需要在雙引號外,再加一個雙引號以區分。 例如: string sqlString =

C++中輸入字串常用的函式

1、cin 1、cin.get() 2、cin.getline() 3、getline() 4、gets() 5、getchar() 1、cin 用法1:最基本,也是最常用的用法,輸入一個數字: #include <iostream> us

C++ STL string 用法詳解

一、string的初始化 首先,為了在程式中使用string型別,必須包含標頭檔案 <string>。如下: #include <string> 注意這裡不是string.h,string.h是C字串標頭檔案。 string類是一個模板類

C++ STL排序後去重函式

陣列排序 (100/100 分數) 題目描述 輸入n個數,對這n個數去重之後排序,並輸出從小到大排序結果 (友情提示:去重函式unique 排序函式sort 具體函式使用規則請查閱相關文件) 輸入描述 首先輸入n,然後接著輸入n個數。其中1<=n&l

c++ STL中的全排列函式

標頭檔案: #include<algorithm> 函式原型: bool next_permutation(iterator start, iterator end); next_per

c++實現String類(建構函式,解構函式,複製建構函式,各類運算子過載函式的編寫)

編寫類 String 的建構函式,解構函式,複製建構函式  需要過載下面的運算子: 1、<、>、==和!=比較運算子 2、+=連線運算子和賦值運算子 3、<<輸出運算子和>>輸入運算子 String.h #ifndef _STRING

字串替換replace, replaceAll, replaceFirst的區別

replace和replaceAll有些人很容易搞混,因此我在這裡詳細講述下。 replace的引數是char和CharSequence,即可以支援字元的替換,也支援字串的替換(CharSequence即字串序列的意思,說白了也就是字串); replaceAll的引數是regex,即基於規則

C++ STL string 基本操作

#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<cctype> #include<iostream

c++ stl sort 自定義排序函式cmp要遵循 strict weak ordering

   滿足strict weak ordering的運算子能夠表達其他所有的邏輯運算子(logical operator): <(a, b)  : (a < b) <=(a, b): !(b < a) ==(a, b): !(a <