中文那里有点坑,需要注意
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void print_lines();
void print_lines() {
cout << "------------" << endl;
}
void print_str(const std::string str);
void print_str(const std::string str) {
cout << "字符串:" << str << endl;
}
void print_string_funs(std::string& str);
///常用的字符串函数 添加&引用类型,才能修改 str的原始数据
void print_string_funs(std::string& str) {
int size = str.size();//字符串长度
bool is_empty = str.empty();//是否为空
char str_char = str[0]; //取下标字符
std::string sub_str = str.substr(0, 1);//取值
int find_index = str.find("梁");//取下标,没找到返回-1
std::string new_string = str.replace(0, 1, "test");//从位置 0 开始,替换后面 1 个字符为 test
print_str(new_string);
cout << "字符长度:" << size << endl;
cout << "是否为空:" << is_empty << endl;
cout << "取下标 0 的char:" << str_char << endl;
cout << "取指定位置:" << sub_str << endl;
cout << "取下标梁的位置:" << find_index << endl;
print_lines();
char char_2 = str.at(0);//取下标值,带边界检查,超出范围会报错: libc++abi: terminating due to uncaught exception of type std::out_of_range: basic_string
cout << "查找字符(带边界检查) char 2: " << char_2 << endl;
print_str(str);
//从尾巴查找字符下标
int index_r = str.rfind("梁");
cout << "从尾巴开始查找下标:" << index_r << "." << endl;
//在尾巴添加一个新的字符串
std::string new_append_string = new_string.append("梁典典....");
print_str("尾巴插入字符串: " + new_append_string);
//在指定位置插入字符串
std::string new_insert_string = new_append_string.insert(0, string("欢迎来到 c++的世界"));
print_str(string("指定位置插入字符串:") + new_insert_string);
//删除指定位置的字符串
new_insert_string = new_insert_string.erase(0, 1);
print_str(string("删除指定位置的字符串:") + new_insert_string);
//清空
new_insert_string.clear();
print_str(string("清空后:") + new_insert_string);
new_insert_string = std::string("你好,梁典典,梁梁梁");
print_str(string("重新设置字符串") + new_insert_string);
//c风格的字符串
const char* c_string = new_insert_string.c_str();
print_str(c_string);
print_lines();
//返回指向字符数据的指针
const char* poi = new_insert_string.data();
cout << "返回指向字符数据的指针:" << poi << endl;
print_lines();
///判断是否相等
int is_eq = new_insert_string.compare("你");//返回 0 相等,大于 1 不一样
cout << "是否相等:" << is_eq << endl;
print_lines();
//查找第一个匹配的位置
print_str(new_insert_string);
int pos = new_insert_string.find_first_of("梁");
cout << "查找梁的第一个匹配下标" << pos << ",字符串大小:" << new_insert_string.size() << endl;
print_lines();
}
void string_num_funs();
/// 字符串和数字之间的转换
void string_num_funs() {
//读取字符串中的数字
string data = "100 100.5";
std::istringstream iss(data);
int i;
double d;
iss >> i >> d;
cout << "int value:" << i << endl; // 100
cout << "doubel value:" << d << endl; // 100.5
/**
* int value:100
* doubel value:100.5
*/
//数字转字符串
std::ostringstream ss;
int a = 100;
double b = 100.5;
ss << "哈哈" << i << " " << d;
string result = ss.str();
cout << "result string is:" << result << endl;//result string is:哈哈100 100.5
//同时读写的例子
string my_data_string = "100,200.2";
stringstream ss2(data);
int i2;
int d2;
//从my_data_string读取数据
ss2 >> i2 >> d2;
cout << "i2=" << i2 << ",d2=" << d2 << endl; //i2=100,d2=100
//向stringstream写入数据
ss2.str("");
ss2 << "new ss2 data:" << 50 << " " << 90.1;
string new_data_string_is = ss2.str();
cout << "new data string is:" << new_data_string_is << endl;//new data string is:new ss2 data:50 90.1
}
int main() {
//声明字符串变量
std::string my_string;
//初始化字符串变量
std::string my_str = "hello,梁典典";
//使用+链接字符串
std::string str1 = "hello";
std::string str2 = "梁典典";
std::string full = str1 + str2;
print_string_funs(full);
cout << "full string is : " << full << endl;
print_lines();
string_num_funs();
return 0;
}
输出
$ string
字符串:testello梁典典
字符长度:14
是否为空:0
取下标 0 的char:h
取指定位置:h
取下标梁的位置:5
------------
查找字符(带边界检查) char 2: t
字符串:testello梁典典
从尾巴开始查找下标:8.
字符串:尾巴插入字符串: testello梁典典梁典典....
字符串:指定位置插入字符串:欢迎来到 c++的世界testello梁典典梁典典....
字符串:删除指定位置的字符串:��迎来到 c++的世界testello梁典典梁典典....
字符串:清空后:
字符串:重新设置字符串你好,梁典典,梁梁梁
字符串:你好,梁典典,梁梁梁
------------
返回指向字符数据的指针:你好,梁典典,梁梁梁
------------
是否相等:1
------------
字符串:你好,梁典典,梁梁梁
查找梁的第一个匹配下标7,字符串大小:26
------------
full string is : testello梁典典
------------
int value:100
doubel value:100.5
result string is:哈哈100 100.5
i2=100,d2=100
new data string is:new ss2 data:50 90.1