C++系列十五:字符串

简介: 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;
}
相关文章
|
1月前
|
存储 算法 编译器
【C++ 字符数组的模板特化】面向字符串的C++模板特化:理解与实践
【C++ 字符数组的模板特化】面向字符串的C++模板特化:理解与实践
47 1
|
1月前
|
对象存储 C++
在C++语言中字符串流
在C++语言中字符串流
18 2
|
1月前
|
并行计算 Go C++
2182.构造限制重复的字符串(模拟 贪心 优先队列 C++ Go)
【2月更文挑战第19天】2182.构造限制重复的字符串(模拟 贪心 优先队列 C++ Go)
23 1
|
1月前
|
Go C++
【力扣】2696. 删除子串后的字符串最小长度(模拟 栈 C++ Go实现栈)
【2月更文挑战第18天】2696. 删除子串后的字符串最小长度(模拟 栈 C++ Go实现栈)
34 6
|
2月前
|
人工智能 算法 测试技术
【动态规划】【字符串】【C++算法】940. 不同的子序列 II
【动态规划】【字符串】【C++算法】940. 不同的子序列 II
|
2月前
|
机器学习/深度学习 算法 C语言
【编码狂想】深度探索C++编程之旅:“数组、字符串、函数与KMP算法解密“
【编码狂想】深度探索C++编程之旅:“数组、字符串、函数与KMP算法解密“
73 0
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
11天前
|
安全 C++
石头剪子布(字符串解法 C++)
石头剪子布(字符串解法 C++)
17 0
|
1月前
|
安全 Unix Linux
【C/C++ 字符串】探索C语言之字符串分割函数:strtok和strsep的区别
【C/C++ 字符串】探索C语言之字符串分割函数:strtok和strsep的区别
16 0
|
1月前
|
存储 Shell C语言
【C/C++ 字符串与整型转换函数】探索C语言中的字符串和整型之间的转换函数
【C/C++ 字符串与整型转换函数】探索C语言中的字符串和整型之间的转换函数
15 0