简介
实际开发中,经常会对字符串进行一些相关操作。其实字符串操作的方式很多,每次都要重新去查资料确认下,现整理相关字符串功能记录,方便后续使用查找。
当前整理的字符串操作有:
- 字符串分割
- 字符串和数字的转换
- 字符串查找子串
- 字符串替换
- 字符串大小写转换
字符串分割
将字符串拆分为子字符串。具体代码见下:
#include <vector>
#include <regex>
#include <string>
std::vector<std::string> splitStr(const std::string &str, const std::string &delim)
{
try
{
std::regex regex(delim);
return std::vector<std::string>{
std::sregex_token_iterator(str.begin(), str.end(), regex, -1), std::sregex_token_iterator()
};
}
catch(const std::exception &e)
{
return std::vector<std::string>();
}
}
数字转字符串
-
方式一
C++11开始支持,在中定义:
std::string to_string( int value ); std::string to_string( long value ); std::string to_string( long long value ); std::string to_string( unsigned value ); std::string to_string( unsigned long value ); std::string to_string( unsigned long long value ); std::string to_string( float value ); std::string to_string( double value ); std::string to_string( long double value );
使用演示:
#include <iostream> #include <string> int main() { double f = 23.43; std::string str = std::to_string(f); std::cout << str << std::endl; }
-
方式二
C++11开始支持,在中定义,使用演示:
#include <sstream> #include <iostream> void main() { std::ostringstream outStream; //outStream << 100; //outStream << true; outStream << 1.2; std::cout << outStream.str() << std::endl; }
-
方式三
c库函数,在<stdlib.h>中定义:
char* _itoa_s(int _Value, har* _Buffer, _Radix); char* _itoa_s(int _Value, har* _Buffer, size_t _BufferCount, _Radix); char* _ltoa_s(long _Value, har* _Buffer, _Radix); char* _ltoa_s(long _Value, har* _Buffer, size_t _BufferCount, _Radix); char* _ultoa_s(unsigned long _Value, har* _Buffer, _Radix); char* _ultoa_s(unsigned long _Value, har* _Buffer, size_t _BufferCount, _Radix);
使用演示:
#include <iostream> #include <stdlib.h> int main() { char str[10] = {0}; _itoa_s(100, str, 10); std::cout << str << std::endl; int size = 20; char* str2 = new char[size]; // 101:转换的数字 // str2:转换的结果 // 10:指代10进制 _itoa_s(101, str2, size, 10); std::cout << str2 << std::endl; delete[] str2; return 0; }
**备注:**缺少浮点型转字符串方式。
字符串转数字
-
方式一
C++11开始支持,在中定义:
// 将一个字符串转换为无符号整数 unsigned long stoul( const std::string& str, size_t *pos = 0, int base = 10 ); unsigned long long stoull( const std::string& str, size_t *pos = 0, int base = 10 ); // 将一个字符串转换为有符号的整数 int stoi( const std::string& str, size_t *pos = 0, int base = 10 ); long stol( const std::string& str, size_t *pos = 0, int base = 10 ); long long stoll( const std::string& str, size_t *pos = 0, int base = 10 ); // 将一个字符串转换为一个浮点值,pos指定解析错误的位置 float stof( const std::string& str, size_t *pos = 0 ); double stod( const std::string& str, size_t *pos = 0 ); long double stold( const std::string& str, size_t *pos = 0 );
使用演示:
#include <iostream> #include <string> int main() { try { size_t pos = 0; // 1:pos表示解析字符串的位置 // 2:可能抛出异常,比如a123.1会出现异常invalid stof argument auto fValue1 = std::stof("123.1", &pos); std::cout << "value:" << fValue1 << ",pos:" << pos << std::endl; } catch(const std::exception &e) { std::cout << "ex:" << e.what() << std::endl; } }
-
方式二
C++11开始支持,在中定义,使用演示:
#include <sstream> #include <string> #include <iostream> int main() { std::string s = "101"; std::istringstream inStream(s); int iValue = -1; inStream >> iValue; std::cout << iValue << std::endl; return 0; }
-
方式三
c库函数,在<stdlib.h>中定义:
// 把参数 str 所指向的字符串转换为一个整数(类型为 int 型)。 int atoi(const char *str); // 把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)。 long int atol(const char *str); long int strtol(const char *str, char **endptr, int base); // 把参数 str 所指向的字符串转换为一个无符号长整数(类型为 unsigned long int 型)。 unsigned long int strtoul(const char *str, char **endptr, int base); // 把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。 double atof(const char *str); double strtod(const char *str, char **endptr); // // 把参数 str 所指向的字符串转换为long long long long atoll(const char *str);
使用演示
#include <iostream> #include <stdlib.h> int main() { auto iValue = atoi("100"); std::cout << iValue << std::endl; }
字符串查找
C++11开始支持,在中定义:
size_type find(_In_z_ const _Elem* const _Ptr, const size_type _Off = 0);
-
是否包含子串
#include <string> #include <iostream> int main() { std::string s{"hello"}; auto hasStr = (s.npos != s.find("el")); std::cout << hasStr << std::endl; }
-
是否以某子串开始
#include <string> #include <iostream> int main() { std::string s{"hello"}; auto isBegin = (0 == s.find("hea")); std::cout << isBegin << std::endl; }
-
是否以某子串结束
#include <string> #include <iostream> int main() { std::string s{"hello"}; std::string subS{"lo1"}; auto isEnd = ((s.length() - subS.length()) == s.find(subS)); std::cout << isEnd << std::endl; }
字符串替换
C++11开始支持,在中定义:
// 替换从_Off开始的_Nx个字符为字符串_Ptr
basic_string& replace(const size_type _Off, const size_type _Nx, _In_z_ const _Elem* const _Ptr);
// 替换从_Off开始的_Nx个字符为_Count个_Ch
basic_string& replace(const size_type _Off, size_type _Nx, const size_type _Count, const _Elem _Ch);
// 将字符串从_First到_Last的字符替换为_Ptr
basic_string& replace(const const_iterator _First, const const_iterator _Last, _In_z_ const _Elem* const _Ptr);
// 将字符串从_First到_Last的字符替换为_Count个_Ch
basic_string& replace(const const_iterator _First, const const_iterator _Last, const size_type _Count, const _Elem _Ch);
-
原始替换:
#include <string> #include <iostream> int main() { std::string str{"The quick brown fox jumps over the lazy dog."}; str.replace(10, 5, "red"); // (5) str.replace(str.begin(), str.begin() + 3, 1, 'A'); // (6) } // 输出结果为:A quick red fox jumps over the lazy dog.
-
封装替换:
#include <string> #include <iostream> std::size_t replaceAll(std::string& inout, const std::string &what, const std::string &with) { std::size_t count{}; for (std::string::size_type pos{}; inout.npos != (pos = inout.find(what.data(), pos, what.length())); pos += with.length(), ++count) { inout.replace(pos, what.length(), with.data(), with.length()); } return count; } int main() { std::string str{"The quick brown fox jumps over the lazy dog."}; auto count = replaceAll(str, " ", "_"); std::cout << "result:" <<str << ",count:" << count << std::endl; return 0; } // 输出结果为:result:The_quick_brown_fox_jumps_over_the_lazy_dog.,count:8
字符串大小写转换
在中定义
_OutIt transform(const _InIt _First, const _InIt _Last, _OutIt _Dest, _Fn _Func)
使用演示:
#include <string>
#include <iostream>
#include <algorithm>
int main()
{
std::string s{"hello"};
std::transform(s.cbegin(), s.cend(),
s.begin(), // write to the same location
[](unsigned char c) { return std::toupper(c); });
std::cout << s << std::endl;
}