string方法
-
assign
-
功能:完全替换string的值,相对于重新赋值
-
用法
// 使用str替换 string& string::assign (const string& str) // 使用str从str_idx开始长度为str_num的内容替换 string& string::assign (const string& str, size_type str_idx, size_type str_num) // 使用字符串常量替换 string & string::assign (const char* cstr) // 使用字符串常量前chars_len个字符替换, string& string::assign (const char* chars, size_type chars_len) // 使用字符C重复num次替换 string & string::assign (size_type num, char c) // 使用迭代器指定区间替换 template <class InputIterator> string& assign (InputIterator first, InputIterator last)
-
代码示例
-
代码
#include<iostream> using namespace std; int main() { string s1("cqmyg"); string s2("ysdssjtwmydtsgx"); cout << "original s1: " << s1 << endl; s1.assign(s2); cout << "new s1: " << s1 << endl; s1.assign(s2, 5, 8); cout << "new s1: " << s1 << endl; s1.assign("zhrmghg"); cout << "new s1: " << s1 << endl; s1.assign("zhrmghg", 5); cout << "new s1: " << s1 << endl; s1.assign(6, 'x'); cout << "new s1: " << s1 << endl; s1.assign(s2.begin() + 2, s2.end()); cout << "new s1: " << s1 << endl; return 0; }
-
输出
original s1: cqmyg new s1: ysdssjtwmydtsgx new s1: jtwmydts new s1: zhrmghg new s1: zhrmg new s1: xxxxxx new s1: dssjtwmydtsgx
-
-
-
replace
-
功能:部分替换
-
用法
// 使用str替换范围[pos, pos + cocunt)里的内容 string& replace( size_type pos, size_type count, const string& str ) // 使用字符串cstr替换范围[pos, pos + count)里的内容 string& replace( size_type pos, size_type count, const CharT* cstr ) // 使用字符串str替换范围[first, last)里的内容 string& replace( const_iterator first, const_iterator last,const string& str ) // 使用字符串str[pos2+count2)替换范围[pos, pos+count)里的内容 string& replace( size_type pos, size_type count,const string& str,size_type pos2, size_type count2 ) // 使用字符ch重复count2次替换范围[pos, pos+count)里的内容 string& replace( size_type pos, size_type count, size_type count2, CharT ch )
-
代码示例
-
代码
#include<iostream> using namespace std; int main() { string s1 = "demonstration"; string s2 = "introduce"; cout << "raw s1: " << s1 << endl; s1.replace(0, 4, s2); cout << "new s1: " << s1 << endl; s1.assign("demonstration"); s1.replace(2, 7, "wobu"); cout << "new s1: " << s1 << endl; s1.assign("demonstration"); s1.replace(s1.begin() + 2, s1.end() + 6, s2); cout << "new s1: " << s1 << endl; s1.assign("demonstration"); s1.replace(1, 4, s2, 1, 4); cout << "new s1: " << s1 << endl; s1.assign("demonstration"); s1.replace(1, 4, 2, 'x'); cout << "new s1: " << s1 << endl; return 0; }
-
输出
raw s1: demonstration new s1: introducenstration new s1: dewobution new s1: deintroduce new s1: dntrostration new s1: dxxstration
-
-
-
sbustr
-
功能:返回指定范围内的内容用
-
用法
// 返回[pos, pos+count)的内容 string substr (size_t pos, size_t count) const
-
代码示例
-
代码
/************************************************************************* > File Name: 4.substr.cpp > Author: > Mail: > Created Time: Tue 02 Mar 2021 02:05:17 PM CST ************************************************************************/ #include<iostream> using namespace std; int main() { string s1 = "introduction"; cout << s1.substr() << endl; cout << s1.substr(1) << endl; cout << s1.substr(2, 3) << endl; cout << s1.substr(4, 100) << endl; //如果end位置超过size,自动取到末尾停止 return 0; }
-
输出
introduction ntroduction tro oduction
-
-
-
insert
-
指定位置插入字符串
-
用法
// 从index插入字符ch的count份复制 string& insert( size_type index, size_type count, CharT ch ) // 从index插入整个字符串s string& insert( size_type index, const CharT* s ) // 从index插入整个str string& insert( size_type index, const string& str ) // 从index插入字符串s前count个元素 string& insert( size_type index, const CharT* s, size_type count ) // 从index插入str[index_str, index_str+count) string& insert( size_type index, const string& str, size_type index_str, size_type count ) // 从迭代器pos插入字符ch iterator insert( iterator pos, CharT ch ) // 从地带其pos插入迭代器范围[first, last)的内容 iterator insert( const_iterator pos, InputIt first, InputIt last );
-
代码示例
-
代码
#include<iostream> using namespace std; int main() { string s1 = "introduction"; string s2 = "malloc"; cout << "raw s1: " << s1 << endl; s1.insert(1, 5, 'x'); cout << "new s1: " << s1 << endl; s1.assign("introduction"); s1.insert(1, 5, 'x'); cout << "new s1: " << s1 << endl; s1.assign("introduction"); s1.insert(1, "basic"); cout << "new s1: " << s1 << endl; s1.assign("introduction"); s1.insert(1, "basic", 3); cout << "new s1: " << s1 << endl; s1.assign("introduction"); s1.insert(1, s2, 1, 4); cout << "new s1: " << s1 << endl; s1.assign("introduction"); s1.insert(s1.begin() + 5, 'x'); cout << "new s1: " << s1 << endl; s1.assign("introduction"); s1.insert(s1.begin() + 5, s2.begin() + 2, s2.begin() + 4); cout << "new s1: " << s1 << endl; return 0; }
-
输出
raw s1: introduction new s1: ixxxxxntroduction new s1: ixxxxxntroduction new s1: ibasicntroduction new s1: ibasntroduction new s1: iallontroduction new s1: introxduction new s1: introllduction
-
-
-
find
-
功能:查找第一个匹配给定字符串; 返回第一个匹配位置,匹配不成功返回npos
-
用法
// 从pos开始匹配整个str size_type find( const string& str, size_type pos = 0 ) const; // 从[pos, pos+count)匹配字符串s size_type find( const CharT* s, size_type pos, size_type count ) const; // 从pos开始匹配字符串s size_type find( const CharT* s, size_type pos = 0 ) const; // 从pos开始匹配字符ch size_type find( CharT ch, size_type pos = 0 ) const;
-
代码示例
-
代码
#include<iostream> using namespace std; void outn(int n) { if (n == -1) { cout << "not found" << endl; } else { cout << "foud at " << n << endl; } } int main() { string::size_type n; string s1 = "introduction"; string s2 = "controd"; n = s1.find(s2); outn(n); n = s1.find("tro"); outn(n); n = s1.find("tro", 2, 3); outn(n); n = s1.find('d', 10); outn(n); return 0; }
-
输出
not found foud at 2 foud at 2 not found
-
-
-
stoi
-
功能:将字符串转换为整型数字
-
用法
// 转换字符串为整形数字, 进制为base,pos存储转换字符个数 int stoi( const std::string& str, std::size_t* pos = 0, int base = 10 );
-
代码示例
-
代码
#include<iostream> using namespace std; int main() { string s1 = "45"; string s2 = "3.144534"; string s3 = "324sdfadf"; string s4 = "sdf222"; size_t p = 0; cout << stoi(s1, &p) << " " << p << endl; cout << stoi(s2, &p) << " " << p << endl; cout << stoi(s3, &p) << " " << p << endl; // cout << stoi(s4, &p) << endl; return 0; }
-
输出
45 2 3 1 324 3
-
-