字符串处理【C++基础】

字符串处理

博客源地址:修能的博客

字符串拼接(Concatenation):

  • 使用"+"运算符可以将两个字符串进行拼接。
  • 使用append()函数可以将一个字符串附加到另一个字符串的末尾。
string str1 = "Hello,";
string str2 = "World!";

string str_plus = str1 + str2;
string str_append = str1.append(str2);
cout << str_plus << endl;
cout << str_append <<  endl;
Hello,World!
Hello,World!

字符串长度(Length):

  • 使用length()函数可以获取字符串的长度。
  • 使用size()函数也可以获取字符串的长度。
string str = "Hello,World!";
cout << str.length();
cout << str.size();
12
12

字符串比较(Comparison):

  • 使用==运算符可以比较两个字符串是否相等。

        string str1 = "Hello";
        string str2 = "hello";
        string str3 = "Hello";
    
        printf("%s", str1 == str2 ? "Same\n" : "Different\n");
        printf("%s", str1 == str3 ? "Same\n" : "Different\n");
    
      Different
      Same
    
  • 使用compare()函数可以比较两个字符串的大小关系。

    string str1 = "ABCD";
    string str2 = "ABCd";
    
    printf("%s", str1.compare(str2) ? "Longer\n" : "Shorter\n");
    
    Longer
    

子串查找(Substring Searching):

  • 使用find()函数可以在字符串中查找指定子串的位置。

    string str = "Hello,World!";
    string substr = "World";
    
    cout << str.find(substr);
    
    6
    
  • 使用rfind()函数可以从字符串末尾开始查找指定子串的位置。

    string str = "Hello,World!";
    string substr = "l";
    
    cout << str.find(substr) << endl;
    cout << str.rfind(substr) << endl;
    
    2
    9
    

子串提取(Substring Extraction):

  • 使用substr(start,end) or substr(position)函数([start,end) or [positon,end))可以从字符串中提取指定位置和长度的子串,end默认为字符串末尾后一位。

    string str = "Hello,World!";
    
    string substr1 = str.substr(0, 5);
    string substr2 = str.substr(5);
    cout << substr1 << endl;
    cout << substr2 << endl;
    
    Hello
    ,World!
    

字符串分割(String Splitting):

  • 使用find_first_of()substr()函数可以实现字符串的分割。

    find_first_of() 是一个字符串成员函数,用于在字符串中查找第一个与给定字符序列中的任何字符匹配的字符的位置。

    string str = "Hello,World!";
    
    int index = str.find_first_of("Wor");
    cout << index << endl;
    string substr1 = str.substr(0,index);
    string substr2 = str.substr(index);
    cout << substr1 << endl;
    cout << substr2 << endl;
    
    4
    Hell
    o,World!
    

字符串替换(String Replacement):

  • 使用``replace()`函数可以将字符串中的指定子串替换为另一个字符串。

    replace() 函数有多个重载版本,根据不同的参数类型和个数,可以实现不同的字符串替换操作。以下是其中一些常用的重载版本:

    1. 替换指定位置的子串:

      std::string& replace(size_t pos, size_t len, const std::string& str);
      
      string str = "Hello,World!";
      cout << str << endl;
      string substr = "Wo";
      string new_str = "wor";
      str.replace(str.find(substr), new_str.length(), new_str);
      
      Hello,World!
      Hello,world!
      
    2. 替换指定位置的子串,或插入/删除内容:

      std::string& replace(size_t pos, size_t len, const std::string& str, size_t subpos, size_t sublen);
      
      string str = "Hello,World!";
      cout << str << endl;
      
      string substr = "Wor";
      string new_str = "wor";
      str.replace(str.find(substr), substr.length(), new_str, 0, new_str.length());
      
      cout << str << endl;
      
      Hello,World!
      Hello,world!
      
    3. 替换指定位置的子串,或插入/删除内容(C风格字符串作为参数)

      std::string& replace(size_t pos, size_t len, const char* s);
      
    4. 替换指定位置的子串,或插入/删除内容(C风格字符串作为参数)

      std::string& replace(size_t pos, size_t len, const char* s, size_t n);
      

字符串大小写转换(Case Conversion):

  • 使用tolower()函数可以将字符串转换为小写形式。

    string ToLower(string str){
        string temp = str;
        for (auto &ch : temp)
        {
            ch = tolower(ch);
        }
        return temp;
    }
    
    int main()
    {
        string str = "Hello,World!";
        
        string newstr = ToLower(str);
    
        cout << str << endl;
        cout << newstr << endl;
        return 0;
    }
    
    Hello,World!
    hello,world!
    
  • 使用toupper()函数可以将字符串转换为大写形式。

    string ToUpper(string str){
        string temp = str;
        for (auto &ch : temp)
        {
            ch = toupper(ch);
        }
        return temp;
    }
    
    int main()
    {
        string str = "Hello,World!";
        
        string newstr = ToUpper(str);
        cout << str << endl;
        cout << newstr << endl;
        return 0;
    }
    
    Hello,World!
    HELLO,WORLD!
    

字符串与数字的相互转换:

  • 使用stoi()函数可以将字符串转换为整数。

    string str_num = "12345";
    int num = stoi(str_num);
    cout << num;
    
    12345
    
  • 使用stof()函数可以将字符串转换为浮点数。

    string str_num = "12345.33";
    double num = stof(str_num);
    
    cout << setprecision(8) <<num;
    
    12345.33
    
  • 使用to_string()函数可以将数字转换为字符串。

    int num = 10;
    double fnum = 3.14;
    cout << to_string(num) << endl;
    cout << to_string(fnum) << endl;
    
    10
    3.140000
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值