RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
c++string类的常用方法

今天小编就为大家带来一篇介绍c++中string类的常用方法的文章。小编觉得挺实用的,为此分享给大家做个参考。一起跟随小编过来看看吧。

成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、网站制作、固安网络推广、微信小程序开发、固安网络营销、固安企业策划、固安品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供固安建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

1、获取字符串长度

 #include
#include
#include
using namespace std;
int main()
{
    string str1 = "hello";
 
    int length = str1.length();
    printf("调用str.length()函数获取字符串长度:%d\n\n",length );
    return 0;
}

2、字符串连接

 #include
#include
#include
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连接结果:"<

3、字符串比较

 #include
#include
#include
using namespace std;
int main()
{
    string str1 = "hello";
    string str2="my girl!";
    string str3="hello ";
 
    if (str1 < str3)
        cout << "字符串比较结果:" << "str1

4、字符串转字符数组

 #include
#include
#include
#include
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++中string类的常用方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果喜欢这篇文章,不如把它分享出去让更多的人看到。


网站标题:c++string类的常用方法
地址分享:http://sczitong.cn/article/gjcdhi.html