C++字符串string的使用 有关string的C++库函数,有很多对字符串操作相关的库函数,这里主要是学习一些个别对字符串操作的方法,在以后的使用中可以通过查询库函数的方法来查询相关的对字符串操作的函数,相关的网站有http://www.cplusplus.com/reference/ https://zh.cppreference.com/w/%E9%A6%96%E9%A1%B5 示例分析:
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string s1, s2, s3;
s1 = "hello cpp";
s2 = "hello world";
cout << "s1 is: " << s1 << endl;
cout << "s2 is: " << s2 << endl;
if(s3.empty())
{
cout << "s3 is empty" << endl;
}
else
{
cout << "s3 is no empty" << endl;
}
cout << "s1'length is " << s1.length() << endl;
s3 += s1;
cout << "s3 is: " << s3 << endl;
s2.insert(0,2,'s');
cout << "after insert, s2 is: " << s2 << endl;
return 0;
}
|