C 中string substr函式的使用方法

2021-08-30 19:32:20 字數 408 閱讀 4490

substr函式的形式為s.substr(pos, n),

需要兩個引數,第乙個是開始位置,第二個是獲取子串的長度。

函式可以從乙個字串中獲取子串,返回乙個string,包含s中從pos開始的n個字元的拷貝(pos的預設值是0,n的預設值是s.size() - pos,即不加引數會預設拷貝整個s),可以作為一種構造string的方法。

若pos的值超過了string的大小,則substr函式會丟擲乙個out_of_range異常;

若pos+n的值超過了string的大小,則substr會調整n的值,只拷貝到string的末尾。

#include#includeusing namespace std;

int main()

輸出結果為:12345

C 中indexof和substring函式用法

c 中indexof和substring函式用法 abcdefg substring 4,2 返回的值為 ef 從字串 abcdefg 中第4位開始取,取到第2位。abcdefg substring 4 返回 efg 從字串 abcdefg 中第4位開始取,取到字串的尾部 code 處理鏈結 add...

C 中indexof和substring函式用法

一 substring用法 abcdefg substring 4,2 返回的值為 ef 從字串 abcdefg 中第4位開始取,取到第2位。abcdefg substring 4 返回 efg 從字串 abcdefg 中第4位開始取,取到字串的尾部 二 indexof lastindexof用法 ...

MATLAB中fopen fprintf函式的用法

matlab中fopen函式在指定檔案開啟的例項如下 1 fopen 開啟檔案,賦予檔案代號。語法1 fid fopen filename,permission 用指定的方式開啟檔案 fid n n是正整數 表示檔案開啟成功,檔案代號是n.fid 1 表示檔案開啟不成功。fid在此次檔案關閉前總是有...