c 字串函式用法舉例

2022-02-19 01:23:52 字數 1283 閱讀 1254

字串切割: substr

函式原型:

string substr ( size_t pos = 0, size_t n = npos ) const;

解釋:抽取字串中從pos(預設為0)開始,長度為npos的子字串

#include #include 

using

namespace

std;

intmain()

結果

hello

lloll

llo

注意:string 類將npos定義為保證大於任何有效下標的值

字串替換:replace

函式原型:

basic string& replace(size_ type pos1, size_type num1, const type* ptr ); 

basic string& replace(size_ type pos1, size_type num1,const string str );

替換用ptr(或str)替換字串從pos1開始的num1個位置

#include #include 

using

namespace

std;

intmain()

結果:hmmmmmlo

#include #include 

using

namespace

std;

intmain()

結果:h12345lo

舉例

split()函式

#include #include 

using

namespace

std;

vector

split(const

string &s, const

string

&delim)

while (pos elems.push_back(s.substr(pos, find_pos-pos));

pos = find_pos +delim_len;

}return

elems;

}int

main()

string字串函式用法

申明 str1 str2為string型別,c1,c2為字元陣列 1.字串長度 str1.length strlen c1 2.字串後加文字 對應c函式strcat c1,c2 返回指標c1 3.字串和字元陣列相互轉換 字元陣列轉化為字串 str1 c1 或者 string str1 c1,c1 n...

python字串函式及用法 字串函式用法彙總

初學python,整理字串常用函式 解決英文單詞大小寫問題 capitalize 將字串首字母大寫 title 返回乙個滿足標題格式的字串 swapcase 將字串中的大小寫字母同時進行互換 lower 大寫轉小寫 upper 小寫轉大寫 解決字串填充問題 center 返回乙個長度為width,兩...

C 字串的用法

string str 我們一起學土撥鼠叫 將字串轉為陣列 返回型別為char char chararr str.toarray foreach char item in chararr 插入指定字元到字串中 inzert 插入的索引位置,要插入的字元 string inzertstr str.ins...