整理了一些处理字符串的常用函数。
目录
一、string类中
Constructors | 构造函数,用于字符串初始化 |
Operators | 操作符,用于字符串比较和赋值 |
append() | 在字符串的末尾添加文本 |
assign() | 为字符串赋新值 |
at() | 按给定索引值返回字符 |
begin() | 返回一个迭代器,指向第一个字符 |
c_str() | 将字符串以C字符数组的形式返回 |
capacity() | 返回重新分配空间前的字符容量 |
compare() | 比较两个字符串 |
copy() | 将内容复制为一个字符数组 |
data() | 返回内容的字符数组形式 |
empty() | 如果字符串为空,返回真 |
end() | 返回一个迭代器,指向字符串的末尾。(最后一个字符的下一个位置) |
erase() | 删除字符 |
find() | 在字符串中查找字符 |
find_first_of() | 查找第一个与value中的某值相等的字符 |
find_first_not_of() | 查找第一个与value中的所有值都不相等的字符 |
find_last_of() | 查找最后一个与value中的某值相等的字符 |
find_last_not_of() | 查找最后一个与value中的所有值都不相等的字符 |
get_allocator() | 返回配置器 |
insert() | 插入字符 |
length() | 返回字符串的长度 |
max_size() | 返回字符的最大可能个数 |
rbegin() | 返回一个逆向迭代器,指向最后一个字符 |
rend() | 返回一个逆向迭代器,指向第一个元素的前一个位置 |
replace() | 替换字符 |
reserve() | 保留一定容量以容纳字符串(设置capacity值) |
resize() | 重新设置字符串的大小 |
rfind() | 查找最后一个与value相等的字符(逆向查找) |
size() | 返回字符串中字符的数量 |
substr() | 返回某个子字符串 |
swap() | 交换两个字符串的内容 |
(1)长度(length)、size、max_size
语法:
size_type length();
size_type size();
size_type max_size();
length()函数返回字符串的长度. 这个数字和size返回的数字相同.
size()函数返回字符串中现在拥有的字符数。
max_size()函数返回字符串能保存的最大字符数。
string str ("Test string");
cout << "size: " << str.size() << "\n";
cout << "length: " << str.length() << "\n";
cout << "max_size: " << str.max_size() << "\n";
/*输出
size: 11
length: 11
max_size: 1073741820
*/
(2)查找 find()
语法:
size_t find (const string& str, size_t index = 0);
size_t find (const char* s, size_t index = 0);
size_t find (const char* s, size_t index, size_t n);
size_t find (char c, size_t index = 0);
find()函数:
- 返回str在字符串中第一次出现的位置(从index开始查找)。如果没找到则返回string::npos,
- 返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos,
- 返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos
例如,
string str1("Alpha Beta Gamma Delta");
unsigned int loc = str1.find("Omega", 0);
if (loc != string::npos)
cout << "Found Omega at " << loc << endl;
else
cout << "Didn't find Omega" << endl;
/*输出
Didn't find Omega
*/
(3)截取 substr()
string substr (size_t index = 0, size_t len = npos) const;
substr()返回本字符串的一个子串,从index开始,长len个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串。
例如:
string s("What we have here is a failure to communicate");
string sub = s.substr(21);
cout << "The original string is : " << s << endl;
cout << "The substring is : " << sub << endl;
/*输出:
The original string is : What we have here is a failure to communicate
The substring is : a failure to communicate
*/
(4)替换 replace()
//用str替换从指定位置index开始长度为len的字符串
string &replace(size_type index, size_type len, const string &str);
//用str的指定子串(给定起始位置subpos和长度sublen)替换从指定位置index上长度为len的字符串
string &replace(size_type index, size_type len, const string &str, size_type subpos, size_type sublen);
//用str替换从指定位置index开始长度为len的字符串
string &replace(size_type index, size_type len, const char *str);
//用str的前n个字符替换从开始位置index长度为len的字符串
string &replace(size_type index, size_type len1, const char *str, size_type n);
//用重复n次的ch字符替换从指定位置index长度为len的内容
string &replace(size_type index, size_type len, size_type n, char ch);
//用str替换指定迭代器位置(从start到end)的字符串
string &replace(iterator start, iterator end, const string &str);
string &replace(iterator start, iterator end, const char *str);
// 用str的前n个字符替换指定迭代器位置(从start到end)的字符串
string &replace(iterator start, iterator end, const char *str, size_type n);
//用重复n次的ch字符替换从指定迭代器位置(从start到end)的字符串
string &replace(iterator start, iterator end, size_type len, char ch);
例:
// replacing in a string
#include <iostream>
#include <string>
using namespace std;
int main()
{
string base = "this is a test string.";
string str2 = "n example";
string str3 = "sample phrase";
string str4 = "useful.";
// replace signatures used in the same order as described above:
// Using positions: 0123456789*123456789*12345
string str = base; // "this is a test string."
str.replace(9, 5, str2); // "this is an example string." (1)
str.replace(19, 6, str3, 7, 6); // "this is an example phrase." (2)
str.replace(8, 10, "just a"); // "this is just a phrase." (3)
str.replace(8, 6, "a shorty", 7); // "this is a short phrase." (4)
str.replace(22, 1, 3, '!'); // "this is a short phrase!!!" (5)
// Using iterators: 0123456789*123456789*
str.replace(str.begin(), str.end() - 3, str3); // "sample phrase!!!" (1)
str.replace(str.begin(), str.begin() + 6, "replace"); // "replace phrase!!!" (3)
str.replace(str.begin() + 8, str.begin() + 14, "is coolness", 7); // "replace is cool!!!" (4)
str.replace(str.begin() + 12, str.end() - 4, 4, 'o'); // "replace is cooool!!!" (5)
str.replace(str.begin() + 11, str.end(), str4.begin(), str4.end()); // "replace is useful." (6)
cout << str << '\n';
return 0;
}
/*输出
replace is useful.
*/
(5) 类型转换
①int转string:to_string()
(c++11起)
//1) 把有符号十进制整数转换为字符串,与 std::sprintf(buf, "%d", value) 在有足够大的 buf 时产生的内容相同。
string to_string( int value );
//2) 把有符号十进制整数转换为字符串,与 std::sprintf(buf, "%ld", value) 在有足够大的 buf 时产生的内容相同。
string to_string( long value );
//3) 把有符号十进制整数转换为字符串,与 std::sprintf(buf, "%lld", value) 在有足够大的 buf 时产生的内容相同。
string to_string( long long value );
//4) 把无符号十进制整数转换为字符串,与 std::sprintf(buf, "%u", value) 在有足够大的 buf 时产生的内容相同。
string to_string( unsigned value );
//5) 把无符号十进制整数转换为字符串,与 std::sprintf(buf, "%lu", value) 在有足够大的 buf 时产生的内容相同。
string to_string( unsigned long value );
//6) 把无符号十进制整数转换为字符串,与 std::sprintf(buf, "%llu", value) 在有足够大的 buf 时产生的内容相同。
string to_string( unsigned long long value );
//7,8) 把浮点值转换为字符串,与 std::sprintf(buf, "%f", value) 在有足够大的 buf 时产生的内容相同。
string to_string( float value );
string to_string( double value );
//9) 把浮点值转换为字符串,与 std::sprintf(buf, "%Lf", value) 在有足够大的 buf 时产生的内容相同。
string to_string( long double value );
参数:
value - 需要转换的数值
返回值:
一个包含转换后值的字符串
异常:
可能从 std::string 的构造函数抛出 std::bad_alloc 。
注意:
对于浮点类型, std::to_string 可能产生不期待的结果,因为返回的字符串中的有效位数能为零,见示例。
返回值可以明显地有别于 std::cout 所默认打印的结果,见示例。
std::to_string 由于格式化目的依赖本地环境,从而从多个线程同时调用 std::to_string 可能会导致调用的部分序列化结果。 C++17 提供高性能、不依赖本地环境的替用品 std::to_chars 。
例:
#include <iostream>
#include <string>
using namespace std;
int main()
{
double f = 23.43;
double f2 = 1e-6;
double f3 = 1e40;
double f4 = 1e-40;
double f5 = 123456789;
string f_str = to_string(f);
string f_str2 = to_string(f2); // 注意:返回 "0.000000"
string f_str3 = to_string(f3); // 注意:不返回 "1e+40".
string f_str4 = to_string(f4); // 注意:返回 "0.000000"
string f_str5 = to_string(f5);
cout << "cout: " << f << '\n'
<< "to_string: " << f_str << "\n\n"
<< "cout: " << f2 << '\n'
<< "to_string: " << f_str2 << "\n\n"
<< "cout: " << f3 << '\n'
<< "to_string: " << f_str3 << "\n\n"
<< "cout: " << f4 << '\n'
<< "to_string: " << f_str4 << "\n\n"
<< "cout: " << f5 << '\n'
<< "to_string: " << f_str5 << '\n';
system("pause");
return 0;
}
/*输出:
std::cout: 23.43
to_string: 23.430000
std::cout: 1e-06
to_string: 0.000001
std::cout: 1e+40
to_string: 10000000000000000303786028427003666890752.000000
std::cout: 1e-40
to_string: 0.000000
std::cout: 1.23457e+08
to_string: 123456789.000000
*/
②string转int :stoi()
(c++11起)
int stoi( const string& str, size_t* pos = 0, int base = 10 );
int stoi( const wstring& str, size_t* pos = 0, int base = 10 );
long stol( const string& str, std::size_t* pos = 0, int base = 10 );
long stol( const wstring& str, std::size_t* pos = 0, int base = 10 );
long long stoll( const string& str, std::size_t* pos = 0, int base = 10 );
long long stoll( const wstring& str, std::size_t* pos = 0, int base = 10 );
跳过所有空白符(以调用 isspace()
鉴别),直到找到首个非空白符,然后取尽可能多的字符组成进制 n (其中 n=base )的整数表示,并将它们转换成一个整数值。合法的整数值由下列部分组成:
进制的合法集是 {0,2,3,...,36} 。合法数字集对于 2进制 整数是 {0,1
},对于3进制整数是 {0,1,2
} ,以此类推。对于大于 10
的进制,合法数字包含字母字符,从对于 11进制的 整数 Aa
到对于36进制的 整数 Zz
。忽略字符大小写。
当前安装的 C 本地环境可能接受另外的数字格式。
若 base 为 0 ,则自动检测数值进制:若前缀为 0
,则底为八进制,若前缀为 0x
或 0X
,则底为十六进制,否则底为十进制。
若符号是输入序列的一部分,则对从数字序列计算得来的数字值取反,如同用结果类型的一元减。
若 pos
不是空指针,则指针 ptr
——转换函数内部者——将接收 str.c_str() 中首个未转换字符的地址,将计算该字符的下标并存储之于 *pos
,该对象给出转换所处理的字符数。
参数:
返回值:
转换到指定有符号整数类型的字符串。
异常:
例:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "45";
string str2 = "3.14159";
string str3 = "31337 with words";
string str4 = "words and 2";
int myint1 = stoi(str1);
int myint2 = stoi(str2);
int myint3 = stoi(str3);
// 错误: 'invalid_argument'
// int myint4 = stoi(str4);
cout << "stoi(\"" << str1 << "\") is " << myint1 << '\n';
cout << "stoi(\"" << str2 << "\") is " << myint2 << '\n';
cout << "stoi(\"" << str3 << "\") is " << myint3 << '\n';
//cout << "stoi(\"" << str4 << "\") is " << myint4 << '\n';
system("pause");
}
/*输出
stoi("45") is 45
stoi("3.14159") is 3
stoi("31337 with words") is 31337
*/
③string转char[]:c_str()、data()
语法:
const char *c_str();
const char *data();
c_str()函数返回一个指向正规C字符串的指针, 内容与本字符串相同,返回的字符串以空字符终止.
C++11起,c_str() 与 data() 进行同一功能。
string str = "Hello World";
char ch[20];
strcpy(ch, str.c_str());
cout << ch << endl;
/*
显示
Hello World
*/
④char[]转string:=
char ch[] = "Hello World";
string str = ch;
cout << str << endl;
/*输出
Hello World
*/
二、algorithm类中
(1)反转 reverse()
语法:
void reverse (BidirectionalIterator first, BidirectionalIterator last);
reverse函数用于反转在[first,last)范围内的顺序(包括first指向的元素,不包括last指向的元素),reverse函数无返回值
string str="hello world";
reverse(str.begin(),str.end());
/*
str结果为 dlrow olleh
*/