在CentOS系统中,C++字符串处理主要依赖于C++标准库中的<string>头文件。以下是一些常用的C++字符串处理方法:
#include <iostream>
#include <string>
std::string str = "Hello, World!";
size_t length = str.length();
std::string str1 = "Hello, ";
std::string str2 = "World!";
std::string result = str1 + str2;
if (str1 == str2) {
std::cout << "Strings are equal." << std::endl;
} else {
std::cout << "Strings are not equal." << std::endl;
}
size_t position = str.find("World");
if (position != std::string::npos) {
std::cout << "Substring found at position: " << position << std::endl;
} else {
std::cout << "Substring not found." << std::endl;
}
std::string str = "Hello, World!";
size_t pos = str.find("World");
if (pos != std::string::npos) {
str.replace(pos, 5, "CentOS");
}
std::string str = "apple,orange,banana";
std::stringstream ss(str);
std::string item;
while (std::getline(ss, item, ',')) {
std::cout << item << std::endl;
}
int num = 42;
std::string str = std::to_string(num);
double pi = 3.14159;
std::string str_pi = std::to_string(pi);
std::string str_num = "123";
int num_converted = std::stoi(str_num);
这些是C++中常用的字符串处理方法。在实际编程中,可以根据需要选择合适的方法进行字符串操作。