C 中的substr()函式

2022-04-08 04:57:45 字數 357 閱讀 6570

#include#includeusing namespace std;

int main()

輸出如下:

12345
0. 用途:一種構造string的方法

1. 形式:s.substr(pos, n)

2. 解釋:返回乙個string,包含s中從pos開始的n個字元的拷貝(pos的預設值是0,n的預設值是s.size() - pos,即不加引數會預設拷貝整個s)

3. 補充:若pos的值超過了string的大小,則substr函式會丟擲乙個out_of_range異常;若pos+n的值超過了string的大小,則substr會調整n的值,只拷貝到string的末尾

C 中substr函式的用法

include includeusing namespace std int main string s 12345abcd string a s.substr 0,5 獲得字串s中 從第0位開始的長度為5的字串 預設時的長度為從開始位置到尾 string b s.substr 5,3 獲得字串s中...

C 中substr函式的用法

include include usingnamespacestd intmain 輸出結果為 12345 更多 0.用途 一種構造string的方法 1.形式 s.substr pos,n 2.解釋 返回乙個string,包含s中從pos開始的n個字元的拷貝 pos的預設值是0,n的預設值是s.s...

C 中substr函式的用法

basic string substr size type off 0,size type count npos const 在使用時,可以設定npos 拷貝長度 也可以不設定。include includeusing namespace std int main 返回值乙個string,包含s中從...