C++系列十五:字符串

C++中的字符串是由字符组成的序列。字符串常用于处理文本数据,例如用户输入、文件内容等。C++标准库提供了一个名为std::string的类,用于表示和处理字符串。

1 、创建和初始化C++字符串

在C++中,可以使用多种方式创建和初始化字符串。以下是一些常用的方法:

(1)直接赋值

std::string str = "Hello, World!";

(2)使用构造函数

std::string str("Hello, World!");

(3) 使用字符数组

char arr[] = "Hello, World!";
std::string str(arr);

(4) 使用字符指针

const char* ptr = "Hello, World!";
std::string str(ptr);

2. C++字符串的常用操作

(1)获取字符串长度

使用size()函数可以获取字符串的长度。

std::string str = "Hello, World!";
int length = str.size(); // 长度为13

(2) 连接字符串

使用+运算符或append()函数可以连接两个字符串。

std::string str1 = "Hello";
std::string str2 = "World!";
std::string str3 = str1 + str2; // 结果为"HelloWorld!"
str3.append(str2); // 结果仍为"HelloWorld!"

(3) 访问字符串中的字符

使用索引运算符[]at()函数可以访问字符串中的字符。注意,索引从0开始。

std::string str = "Hello, World!";
char firstChar = str[0]; // 结果为'H'
char secondChar = str.at(1); // 结果为'e',at()函数也可以用于访问字符串中的字符

(4) 字符串比较

使用==!=<>等运算符进行字符串比较。

std::string str1 = "Hello";
std::string str2 = "World";
if (str1 == str2) {
    
    
    std::cout << "str1 and str2 are equal";
} else {
    
    
    std::cout << "str1 and str2 are not equal";
}

3. C++字符串处理函数

(1)字符串切片:substr()函数

std::string str = "Hello, World!";
std::string subStr = str.substr(0, 5); // 结果为"Hello"

(2)字符串替换:replace()函数

std::string str = "Hello, World!";
str.replace(0, 5, "Hi"); // 结果为"Hi, World!"

(3)字符串分割:find()substr()函数

std::string str = "Hello, World!";
size_t pos = str.find(",");
std::string part1 = str.substr(0, pos); // 结果为"Hello"
std::string part2 = str.substr(pos + 1); // 结果为" World!"

4. C++字符串在实际开发中的应用

字符串在C++中广泛应用于各种场景,例如用户输入处理、文件操作、网络通信等。以下是一些示例:

(1)用户输入处理

使用字符串接收用户输入,并进行相应的处理。

#include <iostream>
#include <string>

int main() {
    
    
    std::string input;
    std::cout << "Enter your name: ";
    std::getline(std::cin, input); // 读取一行输入到字符串中
    std::cout << "Hello, " << input << "!"; // 输出问候语
    return 0;
}

(2)文件操作

使用字符串读取或写入文件路径、文件名等。

#include <fstream>
#include <iostream>
#include <string>

int main() {
    
    
    std::string filename = "example.txt"; // 文件名或路径
    std::ofstream outfile(filename); // 创建输出文件流,打开文件进行写入操作
    outfile << "Hello, World!"; // 写入内容到文件中
    outfile.close(); // 关闭文件流,完成写入操作
    return 0;
}

(3)网络通信

在处理网络请求或响应时,通常需要使用字符串来表示和处理文本数据。

#include <iostream>
#include <string>
#include <curl/curl.h>

size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
    
    
    std::stringbuf buf((char*)contents, size * nmemb);
    std::string str = std::string(buf.data(), size * nmemb);
    std::cout << str; // 输出接收到的文本数据
    return size * nmemb;
}

int main() {
    
    
    CURL *curl = curl_easy_init();
    if (curl) {
    
    
        std::string url = "http://example.com"; // 请求的URL
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_perform(curl); // 发送请求并接收响应
        curl_easy_cleanup(curl);
    }
    return 0;
}

(4)字符串格式化

使用字符串来组织和呈现数据,例如日期、时间、货币等。

#include <iostream>
#include <sstream>
#include <iomanip>
#include <ctime>

int main() {
    
    
    std::time_t now = std::time(0); // 获取当前时间
    std::tm* localTime = std::localtime(&now); // 转换为本地时间
    std::ostringstream oss; // 创建输出字符串流
    oss << "Today is " << std::put_time(localTime, "%A %B %d, %Y"); // 格式化输出时间字符串
    std::string formattedDate = oss.str(); // 获取格式化后的字符串
    std::cout << formattedDate; // 输出格式化后的日期字符串
    return 0;
}

猜你喜欢

转载自blog.csdn.net/apr15/article/details/135436807