D29. C++字串
C++ string的特别用法
str.size():字串长度。
str.empty():字串是否为空
str.length():字串的长度
assign(str, start, num):从str的第start个字元取出num个字元来指定给另一字串物件
append(str, start, num):从str的第start个字元取出num个字元来附加至另一字串物件之后
find(str, 0):从引发find()的字串物件中第 0 个字元寻找是否有符合str的子字串
insert(start, str):将str插入引发insert()的字串物件第start个字元之后
#include <iostream> #include <string> using namespace std; int main() { string s1; string s2("Helloworld"); string s3; s1 = s1.assign(s2, 0, 5); cout << "s1: " << s1 << endl; s3 = s3.append(s2, 0, 5); s3 = s3.append(s2, 0, 4); cout << "s3: " << s3 << endl; s3=s2; cout << "寻找s3中的第一个ll: " << s3.find("ll", 0) << endl; cout << "在s3插入字串 ++: " << s3.insert(3, "++") << endl; cout << "s3的长度: " << s3.length() << endl; return 0; }
记得要在开头加上#include