C++语言基础(16)-string类

 

使用 string 类需要包含头文件<string>,下面的例子介绍了几种定义 string 变量(对象)的方法:

复制代码
#include <iostream>
#include <string>


using namespace std;

int main()
{

    string s1;                   // 只定义不初始化,编译器会赋默认值,默认值为"",即空字符串
    string s2 = "C Plus Plus";   // 既定义又初始化,与C风格字符串不同,string结尾没有结束标志'\0'
    string s3 = s2;              // s3定义时使用s2进行初始化,因此s3内容也是"C Plus Plus"
    string s4 (5,'s');           // s4被初始化由5个's'字符组成的字符串,也就是"sssss"

    return 0;
}
复制代码

一.length() 返回字符串长度

string s = "http://www.iosfan.cn";
int len = s.length();
cout<<len<<endl;

注意: 与C不同,string末尾没有'\0'字符,所以length返回的是字符串的真实长度,而不是长度+1

二.c_str() 转换为C风格的字符串

string path = "D:\\demo.txt";
FILE *fp = fopen(path.c_str(), "rt");

三.访问字符串中的字符

复制代码
#include <iostream>
#include <string>
using namespace std;

int main(){
    string s = "1234567890";
    for(int i=0,len=s.length(); i<len; i++){
        cout<<s[i]<<" ";
    }
    cout<<endl;
    s[5] = '5';
    cout<<s<<endl;
    return 0;
}
复制代码

四.字符串拼接

复制代码
#include <iostream>
#include <string>
using namespace std;

int main(){
    string s1 = "first ";
    string s2 = "second ";
    char *s3 = "third ";
    char s4[] = "fourth ";
    char ch = '@';

    string s5 = s1 + s2;
    string s6 = s1 + s3;
    string s7 = s1 + s4;
    string s8 = s1 + ch;
    
    cout<<s5<<endl<<s6<<endl<<s7<<endl<<s8<<endl;

    return 0;
}
复制代码

运行结果:
first second
first third
first fourth
first @

五.insert() 插入字符串

复制代码
#include <iostream>
#include <string>
using namespace std;

int main(){
    string s1, s2, s3;
    s1 = s2 = "1234567890";
    s3 = "aaa";
    s1.insert(5, s3);
    cout<< s1 <<endl;
    s2.insert(5, "bbb");
    cout<< s2 <<endl;
    return 0;
}
复制代码

运行结果

12345aaa67890
12345bbb67890

六 erase() 删除string中的一个子字符串

复制代码
#include <iostream>
#include <string>
using namespace std;

int main(){
    string s1, s2, s3;
    s1 = s2 = s3 = "1234567890";
    s2.erase(5);
    s3.erase(5, 3);
    cout<< s1 <<endl;
    cout<< s2 <<endl;
    cout<< s3 <<endl;
    return 0;
}
复制代码

运行结果:

1234567890
12345
1234590

七 substr() 截取字符中的一个子字符串

复制代码
#include <iostream>
#include <string>
using namespace std;

int main(){
    string s1 = "first second third";
    string s2;
    s2 = s1.substr(6, 6);
    cout<< s1 <<endl;
    cout<< s2 <<endl;
    return 0;
}
复制代码

运行结果:

first second third
second

八 find() 查找某个字符串出现的位置

复制代码
#include <iostream>
#include <string>
using namespace std;

int main(){
    string s1 = "first second third";
    string s2 = "second";
    int index = s1.find(s2,5);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}
复制代码

运行结果
Found at index : 6

九.rfind() 从第二个参数开始往后查找

复制代码
#include <iostream>
#include <string>
using namespace std;

int main(){
    string s1 = "first second third";
    string s2 = "second";
    int index = s1.rfind(s2,6);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}
复制代码

运行结果:

Found at index : 6

十 find_first_of() 查找子字符串首次出现的位置

复制代码
#include <iostream>
#include <string>
using namespace std;

int main(){
    string s1 = "first second second third";
    string s2 = "asecond";
    int index = s1.find_first_of(s2);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}
复制代码

运行结果:

Found at index : 3

 



如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
posted @   夜行过客  阅读(519)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示