C++ String 字符串类

C++(五)String 字符串类

string 是 C++中处理字符串的类,是对 c 语言中字符串的数据和行为的包装。使
对字符串的处理,更简单易用。

1. 定义和初始化


int main(){

    string *ps= new string("hello");
    cout<<ps<<endl; //0xf55e40
    cout<<*ps<<endl; //hello


    //初始化字符串
    string s1="world";
    string s2("next");

    cout<<sizeof(string )<<endl; //32
    cout<<sizeof(s1)<<endl; //32
    cout<<sizeof(s2)<<endl; //32
    cout<<s1<<endl; //world
    cout<<s2<<endl; //next

    //字符串再次赋值
    s1=s2;
    cout<<s1<<endl; //next

    //cin输入字符串
    //字符串会获取输入第一个空格之前的的数据
    //cin>>s1;
    //cout<<s1<<endl; //输入的数据


    //使用getline函数获取输入的所有数据,包括空格
    getline(cin,s1);
    cout<<s1<<endl; //输入的数据
}

2. 字符串比较

string 重载了运算符+ < > = != += 使字符串的操作变的非常简单。其运
算方式类似于普通数值运算,这是运算符重载的好处。


int main() {
    string s="hello world";
    string x="hello world";
    string y="helloworld!";



    cout<<s.size()<<endl; //获取字符串长度  11
    cout<<s.length()<<endl; //获取字符串长度  11
    cout<<s.empty()<<endl; //判断字符串是否为空  0


    //查找字符串
    cout<<s.find("l")<<endl; //查找字符串中第一个出现的位置  2
    cout<<s.find("l",3)<<endl; //查找字符串中从位置3开始出现的第一个位置  3


    //比较字符串
    cout<<s.compare(x)<<endl; //比较两个字符串是否相等  0
    cout<<s.compare(y)<<endl; //比较两个字符串是否相等  -1
    if(s==x){
        cout<<"s==x"<<endl;
    }
    if(s==y){
        cout<<"s==y"<<endl;
    } else{
        cout<<"s!=y"<<endl;
    }

    //截取字符串
    cout<<s.substr(3)<<endl; //获取从位置3开始到结尾的字符串  lo world
    cout<<s.substr(3,5)<<endl; //获取从位置3开始的5个字符  lo wor



    //拼接
    string z=s+x;
    cout<<z<<endl; //hello worldhello world

    y+=s;
    cout<<y<<endl; //helloworld!hello world
    
    //替换
    string a="hello world";
    string b=a.replace(3,5,"good");
    cout<<a<<endl; //hello world
    cout<<b<<endl; //hegoodd world
}

数值与字符串互转函数

value to string

string to_string(int val);
string to_string(unsigned val);
string to_string(unsigned long val);
string to_string(long long val);
string to_string(unsigned long long val);
string to_string(float val);
string to_string(double val);
string to_string(long double val);
    int inta = 1234;
    string str = to_string(inta);
    cout<<str<<endl; //1234
    str += "5678";
    cout<<str<<endl; //12345678

string to value

int stoi(const string& str, size_t* idx = 0, int base = 10);
long stol(const string& str, size_t* idx = 0, int base = 10);
long long stoll(const string& str, size_t* idx = 0, int base = 10);
unsigned long stoul(const string& str, size_t* idx = 0, int base = 10);
unsigned long long stoull(const string& str, size_t* idx = 0, int base = 10);
float stof(const string& str, size_t* idx = 0);
double stod(const string& str, size_t* idx = 0);
long double stold(const string& str, size_t* idx = 0);

函数参数:

  • str: 要转换的字符串。
  • idx: 指向当前解析位置的指针。
  • base: 进制。

返回值:

  • 转换后的数值。
   
    string str = "12345678";
    int inta = stoi(str);
    cout<<inta<<endl; //12345678
    
    string str = "1111a";
    int a  = stoi(str);
    a++;
    cout<<a<<endl;//1112

    string strbin = "-10010110001";
    string strhex= "0x7f";
    int bin = stoi(strbin,nullptr,2);
    int hex = stoi(strhex,nullptr,16);
    cout<<"bin:"<<bin<<endl;
    cout<<"hex:"<<hex<<endl;

string 数组


    string strArray[10] = {
            "0",
            "1",
            "22",
            "333",
            "4444",
            "55555",
            "666666",
            "7777777",
            "88888888",
            "999999999",
    };
    for(int i=0; i<10; i++)
    {
        cout<<strArray[i]<<endl;
    }
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++中,可以使用string来进行字符串匹配。C++string提了一系列成员函数和操作符来进行字符串的比较和匹。 一种常见的字符串匹配方法是使用string的find函数。find函数可以在一个字符串中查找另一个字符串,并返回第一次出现的位置。例如,可以使用以下代码来查找字符串s中是否包含子串sub: ```cpp #include <iostream> #include <string> int main() { std::string s = "Hello, world!"; std::string sub = "world"; size_t found = s.find(sub); if (found != std::string::npos) { std::cout << "子串在位置 " << found << " 处找到了" << std::endl; } else { std::cout << "子串未找到" << std::endl; } return 0; } ``` 另外,还可以使用string的compare函数来进行字符串的比较。compare函数可以比较两个字符串的大小关系,并返回一个整数值表示比较结果。如果两个字符串相等,则返回0;如果第一个字符串小于第二个字符串,则返回一个负数;如果第一个字符串大于第二个字符串,则返回一个正数。例如,可以使用以下代码来比较两个字符串s1和s2: ```cpp #include <iostream> #include <string> int main() { std::string s1 = "abc"; std::string s2 = "def"; int result = s1.compare(s2); if (result == 0) { std::cout << "两个字符串相等" << std::endl; } else if (result < 0) { std::cout << "s1 小于 s2" << std::endl; } else { std::cout << "s1 大于 s2" << std::endl; } return 0; } ``` 除了以上介绍的方法,C++还提供了其他一些字符串匹配的函数和算法,如正则表达式匹配、KMP算法等。具体使用哪种方法取决于你的需求和场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MarkTop1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值