c++中string类的常用方法有哪些

互联网 20-5-13

c++中string类的常用方法如下:

1、获取字符串长度

  #include<cstdio> #include<iostream> #include<string> using namespace std; int main() {     string str1 = "hello";       int length = str1.length();     printf("调用str.length()函数获取字符串长度:%d\n\n",length );     return 0; }
  #include<cstdio> #include<iostream> #include<string> using namespace std; int main() {     string str1 = "hello";     string str2="my girl!";     string str3="hello ";       string str4=str1+str2;     string str5=str3+str2;     cout<<"字符串str1+str2连接结果:"<<str4<<endl;     cout<<endl;     cout<<"字符串str3+str2连接结果:"<<str5<<endl;     return 0; }
  #include<cstdio> #include<iostream> #include<string> using namespace std; int main() {     string str1 = "hello";     string str2="my girl!";     string str3="hello ";       if (str1 < str3)         cout << "字符串比较结果:" << "str1<str2" << endl;     cout << endl;     return 0; }
  #include<cstdio> #include<iostream> #include<string> #include<cstring> using namespace std; int main() {     string str1 = "hello";     string str2="my girl!";     string str3="hello ";       char *d = new char[20];  //因为下一句那里不是直接赋值,所以指针类型可以不用const char *     strcpy(d, str3.c_str());  //c_str 取得C风格的const char* 字符串     cout << "str3:" << c << endl;     cout << "d:" << d << endl;     str3 = "hahaha";     cout << "str3:" << c << endl;     cout << "d:" << d << endl;     return 0; }

推荐教程:c语言教程

以上就是c++中string类的常用方法有哪些的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
标签: 方法
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:c语言实现输出所有的水仙花数

相关资讯