在C++中string操作较为麻烦,为此整理了一些常用的操作代码,包括字符串拼接、替换、分割、类型转换、截取等等。
1、字符串初始化
std::string str1;
std::string str2 = "hello word";
std::string str3("c++ code");
2、字符串输入输出
//std::string str1;//定义一个字符串str
std::cin >> str1; //使用cin对字符串进行赋值
std::cout <<"输入:" << str1 << std::endl;//使用cout输出字符串
3、字符串类型转换
str->int str->float str->double //通过构建输入流的形式实现
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
template <class T>
void convertFromString(T& value, const std::string& s) {
std::stringstream ss(s);
ss >> value;
}
template <class T>
void convertFromString2Double(T& value, const std::string& s) {
std::stringstream ss(s);
ss.precision(s.length());
ss >> value;
}
4、数字转换为字符串
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
template <class T>
std::string convertToString(T& value)
{
std::ostringstream oss;
oss << value;
std::string str(oss.str());
return str;
}
5、字符串分割
更多分割方法,比如正则表达式,可以参考:https://blog.csdn.net/birenxiaofeigg/article/details/115464934
std::vector<std::string> split(std::string str, std::string pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
str += pattern;//扩展字符串以方便操作
int size = str.size();
for (int i = 0; i < size; i++)
{
pos = str.find(pattern, i);
if (pos < size)
{
std::string s = str.substr(i, pos - i);
result.push_back(s);
i = pos + pattern.size() - 1;
}
}
return result;
}
std::vector<std::string> split_char(std::string str, const char split)
{
std::vector<std::string> result;
std::istringstream iss(str); // 输入流
std::string token; // 接收缓冲区
while (getline(iss, token, split)) // 以split为分隔符
{
result.push_back(token);
}
return result;
}
6、字符串替换
std::string string_replace(std::string source, std::string find, std::string replace)
{
std::string::size_type pos = 0;
std::string::size_type a = find.size();
std::string::size_type b = replace.size();
while ((pos = source.find(find, pos)) != std::string::npos)
{
source.replace(pos, a, replace);
pos += b;
}
return source;
}
7、字符串拼接
std::string string_concat(std::vector<std::string> list,std::string join="") {
std::string result = "";
for (int i = 0;i < list.size()-1;i++) {
result += list[i].append(join);
}
result += list[list.size()-1];
return result;
}
8、字符串位置查找(子串查找)
int start = str.find_first_of("[");
int end = str.find_first_of("]");
9、字符串截取
std::string substr = str.substr(start, end-start+1);//substr(index,length)
10、其他操作
字符串的其他操作 参考来源:https://baijiahao.baidu.com/s?id=1714783064525225407&wfr=spider&for=pc
1、创建一个包含n个元素,每个元素初始化为指定元素例如:string str1(3,A);输出结果会是“AAA”。
2、把str1赋值给str2 :string str2(str1)或者str2=str1;
3、获取字符串的长度和大小使用:str.size()和str.length();
4、字符串追加把str2追加到str1后面:str1+=str2、str1.append(str2)。
5、比较字符串是否相等:str1==str2 或者str1 !=str2;
6、获取str1中的子字符串:str1.substr(n,m),从str1的第n个字符开始取m个字符。
7、获取字符串中某个字符:str[n]、或者str.at(n)获取str字符串中第n个字符。
11、全部代码
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
//字符串类型转换,str->int str->float str->double
//通过构建输入流的形式实现
template <class T>
void convertFromString(T& value, const std::string& s) {
std::stringstream ss(s);
ss >> value;
}
template <class T>
void convertFromString2Double(T& value, const std::string& s) {
std::stringstream ss(s);
ss.precision(s.length());
ss >> value;
}
//将其他类型的数字转换为字符串
template <class T>
std::string convertToString(T& value)
{
std::ostringstream oss;
oss << value;
std::string str(oss.str());
return str;
}
//字符串分割
//更多分割可以参考:https://blog.csdn.net/birenxiaofeigg/article/details/115464934
std::vector<std::string> split(std::string str, std::string pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
str += pattern;//扩展字符串以方便操作
int size = str.size();
for (int i = 0; i < size; i++)
{
pos = str.find(pattern, i);
if (pos < size)
{
std::string s = str.substr(i, pos - i);
result.push_back(s);
i = pos + pattern.size() - 1;
}
}
return result;
}
std::vector<std::string> split_char(std::string str, const char split)
{
std::vector<std::string> result;
std::istringstream iss(str); // 输入流
std::string token; // 接收缓冲区
while (getline(iss, token, split)) // 以split为分隔符
{
result.push_back(token);
}
return result;
}
//字符串替换
std::string string_replace(std::string source, std::string find, std::string replace)
{
std::string::size_type pos = 0;
std::string::size_type a = find.size();
std::string::size_type b = replace.size();
while ((pos = source.find(find, pos)) != std::string::npos)
{
source.replace(pos, a, replace);
pos += b;
}
return source;
}
//字符串拼接
std::string string_concat(std::vector<std::string> list,std::string join="") {
std::string result = "";
for (int i = 0;i < list.size()-1;i++) {
result += list[i].append(join);
}
result += list[list.size()-1];
return result;
}
//字符串位置查找
//int start = str.find_first_of("[");
//int end = str.find_first_of("]");
//字符串截取
//std::string substr = str.substr(start, end-start+1);//substr(index,length)
int main() {
//字符串初始化
std::string str1;
std::string str2 = "hello word";
std::string str3("c++ code");
//字符串输入输出
//std::string str1;//定义一个字符串str
std::cin >> str1; //使用cin对字符串进行赋值
std::cout <<"输入:" << str1 << std::endl;//使用cout输出字符串
std::vector<std::string> list;
list.push_back(str1);
list.push_back(str2);
list.push_back(str3);
std::cout << "str1:" << str1 << std::endl;
std::cout << "str2:" << str2 << std::endl;
std::cout << "str3:" << str3 << std::endl;
std::string s_join=string_concat(list,"--");
std::cout << "字符串(str1,str2,str3)拼接为 s_join :" << s_join << std::endl;
std::vector<std::string> list2 = split(s_join,"--");
std::cout << "字符串 s_join 分割 -- 为:" << list2[0] << ", " << list2[1] << ", " << list2[2] << std::endl;
std::string s_replace = string_replace(s_join, "--", "_");
std::cout << "字符串 s_join 替换 -- 为 _ :" << s_replace << std::endl;
return 0;
}