substr()為string類的一個成員函數:
str.substr(begin,length) 表示切割字符串str,從下標begin處開始,長度為length來切片
示例代碼:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int main() 6 { 7 string a = "abcd"; 8 string tmp = a.substr(0,3); 9 cout << tmp; //輸出結果:abc 10 return 0; 11 }
注意,使用該方法后,原字符串不會改變!