您的位置:

C++字符串操作函数:从不同角度操作字符串

一、字符串的定义和常用操作

C++中的字符串可以使用char数组或者string类实现。char数组是一组字符的集合,可以用字符串字面量或者字符数组初始化。string类则是一个预定义的类,可以直接使用字符串字面量或者字符串进行初始化。以下是常用的字符串操作函数:


#include <iostream>
#include <string>

int main() {
    char str1[] = "Hello";
    char str2[] = {'w', 'o', 'r', 'l', 'd', '\0'};
    std::string str3 = "C++";
    std::string str4 = "Programming";

    // 获取字符串长度
    std::cout << "Length of str1: " << strlen(str1) << std::endl;
    std::cout << "Length of str2: " << strlen(str2) << std::endl;
    std::cout << "Length of str3: " << str3.length() << std::endl;
    std::cout << "Length of str4: " << str4.size() << std::endl;

    // 字符串拼接
    std::cout << "Concatenation of str1 and str2: " << strcat(str1, str2) << std::endl;
    std::cout << "Addition of str3 and str4: " << str3 + " " + str4 << std::endl;

    // 字符串复制
    char str5[10];
    strcpy(str5, str1);
    std::cout << "Copy of str1: " << str5 << std::endl;

    // 字符串查找
    std::cout << "Index of 'o' in str1: " << strchr(str1, 'o') - str1 + 1 << std::endl;
    std::cout << "Last index of 'l' in str1: " << strrchr(str1, 'l') - str1 + 1 << std::endl;
    std::cout << "Index of 'PP' in str3: " << str3.find("PP") + 1 << std::endl;

    return 0;
}

二、字符串的遍历和比较

在进行字符串遍历时,可以使用循环结构进行逐个遍历,也可以使用string类的迭代器进行遍历。字符串的比较可以使用比较运算符进行比较,也可以使用字符串操作函数进行比较。


#include <iostream>
#include <string>

int main() {
    std::string str1 = "This is a C++ string";
    std::string str2 = "C++ is a high-levle programming language";
    std::string str3 = "The quick brown fox jumps over the lazy dog";
    std::string str4 = "The quick brown fox jumps over the lazy dog";

    // 字符串遍历 - 循环
    for (int i = 0; i < str1.length(); i++) {
        std::cout << str1[i] << " ";
    }
    std::cout << std::endl;

    // 字符串遍历 - 迭代器
    for (std::string::iterator it = str2.begin(); it != str2.end(); it++) {
        std::cout << *it << "-";
    }
    std::cout << std::endl;

    // 字符串比较
    if (str3 == str4) {
        std::cout << "str3 is equal to str4" << std::endl;
    } else {
        std::cout << "str3 is not equal to str4" << std::endl;
    }

    if (str1.compare(str2) == 0) {
        std::cout << "str1 is equal to str2" << std::endl;
    } else {
        std::cout << "str1 is not equal to str2" << std::endl;
    }

    return 0;
}

三、字符串的截取和分割

在进行字符串截取时,可以使用substr方法,该方法接受两个参数,第一个参数是开始截取的位置,第二个参数是截取的长度。在进行字符串分割时,可以使用getline方法,该方法接受两个参数,第一个参数是输入流,第二个参数是分隔符。通过循环使用getline方法,可以将字符串分割成多个子字符串。


#include <iostream>
#include <string>
#include <sstream>
#include <vector>

int main() {
    std::string str1 = "C++ is a high-level programming language";
    std::string str2 = "red,green,blue,yellow,black";

    // 字符串截取
    std::cout << "Substring of str1 from the 9th character for 5 characters: " << str1.substr(9, 5) << std::endl;
    std::cout << "Substring of str1 from the 0th character for the first space character: " << str1.substr(0, str1.find(" ")) << std::endl;

    // 字符串分割
    std::stringstream ss(str2);
    std::vector<std::string> vec;
    std::string temp;
    char delimiter = ',';

    while (getline(ss, temp, delimiter)) {
        vec.push_back(temp);
    }

    std::cout << "The original string: " << str2 << std::endl;
    std::cout << "The vector of substrings: ";
    for (auto x : vec) {
        std::cout << x << " ";
    }
    std::cout << std::endl;

    return 0;
}

四、字符串的插入和删除

字符串的插入可以使用insert方法,该方法接受两个参数,第一个参数是插入的位置,第二个参数是插入的字符串。字符串的删除可以使用erase方法,该方法接受两个参数,第一个参数是删除的起始位置,第二个参数是删除的长度。


#include <iostream>
#include <string>

int main() {
    std::string str1 = "This is a C++ string";
    std::string str2 = "Programming";
    std::string str3 = "C";

    // 字符串插入
    std::cout << "Inserting 'great' at position 10: " << str1.insert(10, " great") << std::endl;

    // 字符串删除
    std::cout << "Erasing 7 characters starting from position 8: " << str2.erase(8, 7) << std::endl;

    // 删除指定字符
    str3.erase(std::remove(str3.begin(), str3.end(), 'C'), str3.end());
    std::cout << "Removing all 'C' from str3: " << str3 << std::endl;

    return 0;
}

五、字符串的转换和格式化

在进行字符串转换时,可以使用atoi、atof等函数将字符串转换成数字、浮点数等类型。在进行字符串格式化时,可以使用sprintf函数和C++11后的std::to_string方法进行格式化。


#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iomanip>

int main() {
    char str1[] = "12345";
    char str2[] = "3.1415";
    std::string str3 = "35";
    std::string str4 = "Hello";
    int num1 = atoi(str1);
    double num2 = atof(str2);
    int num3 = std::stoi(str3);
    double num4 = std::stod(str2);

    // 字符串转换
    std::cout << "Converting str1 to an integer: " << num1 << std::endl;
    std::cout << "Converting str2 to a double: " << num2 << std::endl;
    std::cout << "Converting str3 to an integer: " << num3 << std::endl;
    std::cout << "Converting str2 to a double: " << num4 << std::endl;

    // 字符串格式化
    char buffer[50];
    sprintf(buffer, "%-.4f", num2);
    std::cout << "Formatting num2 to 4 decimal places: " << buffer << std::endl;
    std::string str5 = std::to_string(num2);
    std::cout << "Formatting num2 to 2 decimal places: " << std::setprecision(2) << std::fixed << num2 << std::endl;

    return 0;
}