- 1 构造函数
- 2 string对象赋值
3 assign()赋值- 4 求字符串长度
.length()or.size() - 5 string对象中字符串的连接
- 6 string对象的比较
compare() - 7 string子串 substr(pos,n)
- 8 交换两个string内容
- 9 查找子串和字符
- 9 替换子串
replace()返回对对象自身的引用 - 10 删除子串
erase(pos,n) - 11 插入子串
insert(pos, str/char*) - 12 字符串存取 str[i] str.at(i)
- 13 string流处理
#include <sstream> - 14 用 STL 算法操作 string 对象2
1 构造函数
string s();// s = ""string s("hello j"); // s= "hello j"string s(4,'k'); // s = 'kkkk'string s4("12345", 1, 3); //s4 = "234",即 "12345" 的从下标 1 开始,长度为 3 的子串
2 string对象赋值
可以用 char* 类型的变量、常量,以及 char 类型的变量、常量对 string 对象进行赋值。例如:
string s1;
s1 = "Hello"; // s1 = "Hello"
s2 = 'K'; // s2 = "K”
3 assign()赋值
string s1("12345"), s2;
s3.assign(s1); // s3 = s1
s2.assign(s1, 1, 2); // s2 = "23",即 s1 的子串(1, 2) 从1开的的前2个
s2.assign(4, 'K'); // s2 = "KKKK"
s2.assign("abcde", 2, 3); // s2 = "cde",即 "abcde" 的子串[2, 2+3)
4 求字符串长度 .length() or .size()
5 string对象中字符串的连接
除了可以使用+和+=运算符对 string 对象执行字符串的连接操作外,string 类还有 append 成员函数,可以用来向字符串后面添加内容。append 成员函数返回对象自身的引用。
string s1("123"), s2("abc");
s1.append(s2); // s1 = "123abc"
s1.append(s2, 1, 2); // s1 = "123abcbc"添加s2的子串[1,1+2)
s1.append(3, 'K'); // s1 = "123abcbcKKK"添加后面字符串*3
s1.append("ABCDE", 2, 3); // s1 = "123abcbcKKKCDE",添加 "ABCDE" 的子串[2, 2+3)
6 string对象的比较 compare()
除了可以用 <、<=、==、!=、>=、> 运算符比较 string 对象外,string 类还有 compare 成员函数,可用于比较字符串。compare 成员函数有以下返回值:
- 小于 0 表示当前的字符串小;
- 等于 0 表示两个字符串相等;
- 大于 0 表示另一个字符串小。
string s1("hello"), s2("hello, world");
int n = s1.compare(s2); // 比较s1,s2
n = s1.compare(1, 2, s2, 0, 3); //比较s1的子串 (1,2) 和s2的子串 (0,3)
n = s1.compare(0, 2, s2); // 比较s1的子串 (0,2) 和 s2
n = s1.compare("Hello");
n = s1.compare(1, 2, "Hello"); //比较 s1 的子串(1,2)和"Hello”
n = s1.compare(1, 2, "Hello", 1, 2); //比较 s1 的子串(1,2)和 "Hello" 的子串(1,2)
7 string子串 substr(pos,n)
string s1 = "this is ok";
string s2 = s1.substr(2, 4); // s2 = "is i"
s2 = s1.substr(2); // s2 = "is is ok"
8 交换两个string内容
string s1("'west'"), s2("'east'");
s1.swap(s2); // 'east'
9 查找子串和字符
string 类有一些查找子串和字符的成员函数,它们的返回值都是子串或字符在 string 对象字符串中的位置(即下标)。如果查不到,则返回 string::npos。string: :npos 是在 string 类中定义的一个静态常量。这些函数如下:
| find | 从前往后查找子串或字符出现的位置。 |
|---|---|
| rfind | 从后向前查找子串或者字符出现的位置 |
| find_first_of | 从前往后查找何处出现另一个字符串中包含的字符。 |
| s1.find_first_of(“abc”); | //查找s1中第一次出现”abc”中任一字符的位置 |
| find_last_of | 从后往前查找何处出现另一个字符串中包含的字符。 |
| find_first_not_of | 从前往后查找何处出现另一个字符串中没有包含的字符。 |
| find_last_not_of | 从后往前查找何处出现另一个字符串中没有包含的字符。 |
string s1("Source COde");
int n;
if ((n=s1.find('u'))!=string::npos)
{
cout << n << "rd " << s1.substr(n) << endl;
}
if((n = s1.find_first_of("ceo"))!=string::npos)
{
cout << n << "th " << s1.substr(n) << endl;
}
if ((n = s1.find_last_of("ceo")) != string::npos)
{
cout << n << "th " << s1.substr(n) << endl;
}
if ((n = s1.find_first_not_of("ceo")) != string::npos)
{
cout << n << "th " << s1.substr(n) << endl;
}
if ((n = s1.find_last_not_of("ceo")) != string::npos)
{
cout << n << "th " << s1.substr(n) << endl;
}
2rd urce COde
1th ource COde
10th e
0th Source COde
9th de
9 替换子串 replace() 返回对对象自身的引用
string s1("Source COde");
s1.replace(1, 3, "0123456", 1, 3); // 用123替换s1的[1,3]
cout<<s1<<endl;
s1.replace(1, 3, 5, '0'); //用 5 个 '0' 替换子串[1,3]
cout << s1 << endl;
int n = s1.find("00000");
s1.replace(n, 5, "xxxx"); //将子串(n,5)替换为"XXX"
cout<< s1 << endl;
S123ce COde
S00000ce COde
Sxxxxce COde
10 删除子串 erase(pos,n)
string s1("0123 56 8");
s1.erase(0, 5);
56 8
11 插入子串 insert(pos, str/char*)
insert 成员函数可以在 string 对象中插入另一个字符串,返回值为对象自身的引用。
string s1("0123 56 8");
s1.insert(4, "4");
0123 456 8
12 字符串存取 str[i] str.at(i)
13 string流处理 #include <sstream>
#include <sstream>
string src("Avatar 123 5.2 Titanic K");
istringstream istrStream(src); //建立src到istrStream的联系
string s1, s2;
int n; double d; char c;
istrStream >> s1 >> n >> d >> s2 >> c; //把src的内容当做输入流进行读取
ostringstream ostrStream;
ostrStream << s1 << endl << s2 << endl << n << endl << d << endl << c <<endl;
cout << ostrStream.str();
14 用 STL 算法操作 string 对象2
string 对象也可以看作一个顺序容器,它支持随机访问迭代器,也有 begin 和 end 等成员函数。STL 中的许多算法也适用于 string 对象。
string s("abcdefg");
string::iterator p = find(s.begin(), s.end(), 'c');
if (p!=s.end())
{
cout << p - s.begin() << endl; //2
cout << *p << endl; //c
cout << *s.begin() << endl; //a
}
sort(s.begin(), s.end());
cout << s << endl; // abcdefg
next_permutation(s.begin(), s.end()); // 字典序 abcdegf
cout << s << endl;
return 0;
