Appearance
L3_06 字符串及其函数
一、大小写转换
1.1 C++ 中的转换
cpp
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string str = "Hello World!";
// 转大写
for (char &c : str) {
c = toupper(c);
}
cout << str << endl; // HELLO WORLD!
// 转小写
for (char &c : str) {
c = tolower(c);
}
cout << str << endl; // hello world!
return 0;
}1.2 Python 中的转换
python
str = "Hello World!"
print(str.upper()) # HELLO WORLD!
print(str.lower()) # hello world!
print(str.title()) # Hello World!
print(str.capitalize()) # Hello world!二、字符串搜索
2.1 C++ 中的搜索
cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello World!";
// 查找子串
size_t pos = str.find("World");
if (pos != string::npos) {
cout << "找到 'World' 在位置: " << pos << endl;
} else {
cout << "未找到" << endl;
}
// 从指定位置查找
pos = str.find("l", 3);
cout << "从位置3开始查找 'l': " << pos << endl;
return 0;
}2.2 Python 中的搜索
python
str = "Hello World!"
# 查找子串
print(str.find("World")) # 返回索引6
print(str.find("Python")) # 未找到返回-1
# 判断是否包含
print("World" in str) # True
print("Python" in str) # False
# 查找所有出现位置
import re
matches = [match.start() for match in re.finditer('l', str)]
print(matches) # [2, 3, 9]三、字符串分割
3.1 C++ 中的分割
cpp
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
vector<string> split(const string &s, char delimiter) {
vector<string> tokens;
string token;
istringstream tokenStream(s);
while (getline(tokenStream, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
int main() {
string str = "apple,banana,cherry";
vector<string> fruits = split(str, ',');
for (const string &fruit : fruits) {
cout << fruit << endl;
}
return 0;
}3.2 Python 中的分割
python
str = "apple,banana,cherry"
# 使用逗号分割
fruits = str.split(',')
print(fruits) # ['apple', 'banana', 'cherry']
# 使用空格分割
sentence = "Hello World Python"
words = sentence.split()
print(words) # ['Hello', 'World', 'Python']
# 限制分割次数
str = "a,b,c,d,e"
parts = str.split(',', 2)
print(parts) # ['a', 'b', 'c,d,e']四、字符串替换
4.1 C++ 中的替换
cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello World!";
// 替换子串
str.replace(6, 5, "Python"); // 从位置6开始,替换5个字符
cout << str << endl; // Hello Python!
return 0;
}4.2 Python 中的替换
python
str = "Hello World!"
# 替换所有匹配
new_str = str.replace("World", "Python")
print(new_str) # Hello Python!
# 限制替换次数
str = "aaaabbbb"
new_str = str.replace("a", "x", 2)
print(new_str) # xxabbbb五、其他常用操作
5.1 获取长度
cpp
// C++
string str = "Hello";
cout << str.size() << endl; // 5python
# Python
str = "Hello"
print(len(str)) # 55.2 字符串拼接
cpp
// C++
string str1 = "Hello";
string str2 = "World";
string result = str1 + " " + str2;python
# Python
str1 = "Hello"
str2 = "World"
result = str1 + " " + str25.3 字符串切片
cpp
// C++
string str = "Hello World";
string substr = str.substr(0, 5); // 从位置0开始,取5个字符python
# Python
str = "Hello World"
substr = str[0:5] # 从索引0到5(不包含5)六、示例程序
6.1 C++ 字符串处理
cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string email;
cout << "输入邮箱地址: ";
cin >> email;
// 查找@位置
size_t at_pos = email.find('@');
if (at_pos != string::npos) {
string username = email.substr(0, at_pos);
string domain = email.substr(at_pos + 1);
cout << "用户名: " << username << endl;
cout << "域名: " << domain << endl;
} else {
cout << "无效邮箱" << endl;
}
return 0;
}6.2 Python 字符串处理
python
# 统计字符串中每个字符出现的次数
str = "Hello World!"
char_count = {}
for char in str:
char_count[char] = char_count.get(char, 0) + 1
print(char_count)