C String字串處理常用函式

2021-08-21 08:28:34 字數 929 閱讀 1535

下面是幾個string類用於提取字串的常用方法:

1.str.find(str2,pos):從str的pos位開始查詢匹配str2,並返回其在str中第一次出現的位置,pos沒有賦值的話預設為0

string str1="test";

string str2="t";

unsigned int index;

if(str1.find(str2)!=string::nops) //判斷str2是否為str1的子串

2.str.erase(first,last):first和last均為迭代器,刪除[first,last)之間的所有元素,last沒有賦值的話只刪除first處的元素;

str.erase(pos,len):pos為unsigned int 型別,刪除從pos為開始的長度為len的字串;

3.str.substr(pos,len):返回從pos位開始的長度為len的字串;

4.str.insert(pos,str2):在pos位插入str2;

str.insert(it,it1,it2):在str的迭代器it處插入str2從it1到it2的子字串;

下面的程式用於取出括號內逗號相隔的子字串並儲存在map容器中:

// test.cpp: 定義控制台應用程式的入口點。

//#include "stdafx.h"

#include#include#include#include#includeusing namespace std;

void str_deal(string str, mapmp, int &index) }}

int main()

system("pause");

return 0;

}

c string字串常用的方法

tochararray int startindex,int length 引數startindex 此例項內子字串的起始位置。length 此例項內子字串的長度。eg 將字串a複製為字元陣列d 引數value 要搜尋的字串。可判斷字元中是否出現在字串中,返回bool型 引數startindex 插...

C string 字串匹配

c inlcude c include find 在乙個字串中查詢乙個指定的單個字元或字元陣列。如果找到,就返回首次匹配的開始位置 如果沒有查詢到匹配的內容,就返回string npos。find first of 在乙個目標串中進行查詢,返回值是第乙個與指定字元組中任何字元匹配的字元位置。如果沒有...

C string 字串函式

december 8,2012 程式設計指南 和 連線字串 字串賦值 和 字串比較 例如a b,aa ab 比較字串 輸出 輸入字串 注意 使用過載的運算子 時,必須保證前兩個運算元至少有乙個為 string 型別。例如,下面的寫法是不合法的 include include intmain stri...