采用find()方法可查找字符串中的第一个字符元素(char, 用单引号界定)或者子串(用双引号界定);如果查到,则返回下标值(从0开始计数),如果查不到,则返回一个很大的数string:npos(即:4294967295)。
需要区分int和unsigned int两种情况:
(1)int n = str.find("****");
找不到返回的是-1
(2)unsigned int n = str.find("*******");
则返回一个很大的数string:npos(即:4294967295)。
#include <iostream> #include <stdio.h> #include <string> using namespace std; int main(int argc, char* argv[]) { string s; s = "cat dog cat"; //查找第一个字符'c',返回下标值 cout << s.find('c') << endl; //查找第一个子串"c",返回下标值 cout << s.find("c") << endl; //查找第一个子串"cat",返回下标值 cout << s.find("cat") << endl; //查找第一个子串"dog",返回下标值 cout << s.find("dog") << endl; //查找第一个子串"pig",查不到则返回4294967295 cout << s.find("pig") << endl; if(s.find("pig") == string::npos) cout << "没有找到" << endl; return 0; }
#include <iostream> #include <stdio.h> #include <string> using namespace std; int main(int argc, char* argv[]) { string s; s = "cat dog cat"; //使用比较符号">","<","==" cout << (s == "cat dog cat") << endl; //s比"abc"字符串大,返回1 cout << s.compare("abc") << endl; //异常:s比"cat dog c"字符串大,返回2,发现返回值是剩下2个值不一致则返回2. cout << s.compare("cat dog c") << endl; //s比"dog"小,返回-1 cout << s.compare("dog") << endl; //s与"cat dog cat"相等,返回0 cout << s.compare("cat dog cat") << endl; return 0; }
#include <iostream> #include <string> #include <algorithm> using namespace std; int main(int argc, char* argv[]) { string s; s = "123456789"; reverse(s.begin(), s.end()); cout << s << endl; return 0; }
#include <iostream> #include <string> #include <algorithm> using namespace std; int main(int argc, char* argv[]) { vector<string> str; str.push_back("Jack"); str.push_back("Mike"); str.push_back("Tom"); for(int i = 0; i < str.size(); i++) cout << str[i] << endl; cout << str[0][0] << endl; cout << str[1][0] << endl; cout << str[2].size() << endl; return 0; }
#include <iostream> #include <string> using namespace std; int main(int argc, char* argv[]) { string s; s = "1234059"; int sum = 0; for(int i = 0; i < s.length(); i++) { if(s[i] == '0') sum += 0; else if(s[i] == '1') sum += 1; else if(s[i] == '2') sum += 2; else if(s[i] == '3') sum += 3; else if(s[i] == '4') sum += 4; else if(s[i] == '5') sum += 5; else if(s[i] == '6') sum += 6; else if(s[i] == '7') sum += 7; else if(s[i] == '8') sum += 8; else if(s[i] == '9') sum += 9; } cout << sum << endl; return 0; }
#include <iostream> #include <stdio.h> #include <string> using namespace std; int main(int argc, char* argv[]) { string s; char ss[100]; //输入字符串到字符数组中 scanf("%s", &ss); //字符数组赋值字符串对象 s = ss; //用pringf输出字符串对象,要采用c_str()方法 printf(s.c_str()); cout << endl; //用printf输出字符数组 printf("%s", ss); cout << endl; return 0; }
#include <iostream> #include <stdio.h> #include <string> using namespace std; int main(int argc, char* argv[]) { string s1, s2, s3; char sa[100], sb[100], sc[100]; //将字符串分离成数字,分隔符为空格 sscanf("abc 123 pc", "%s %s %s", sa, sb, sc); s1 = sa; s2 = sb; s3 = sc; cout << s1 << " " << s2 << " " << s3 << endl; //将字符串分离成数字,分隔符为空格 //当用到数字的时候,跟scanf一样,它需要传指针地址 int a, b, c; sscanf("1 2 3", "%d %d %d", &a, &b, &c); cout << a << " " << b << " " << c << endl; //当字符串分离成数字,分隔符为","和"$" //当用到数字的时候,跟scanf一样,它要传指针地址 int x, y, z; sscanf("4,5$6", "%d,%d$%d", &x, &y, &z); cout << x << " " << y << " " << z << endl; return 0; }
#include <iostream> #include <sstream> #include <string> #include <stdio.h> using namespace std; //将数值转化为string的第二种方法:C++方法 string convertToString(double x) { ostringstream o; if(o << x) return o.str(); return "error"; } double convertFromString(const string &s) { istringstream i(s); double x; if(i >> x) return x; return 0.0; } int main(int argc, char* argv[]) { //将数值转化为string的第一种方法:C方法 char b[10]; string a; sprintf(b, "%d", 1975); a = b; cout << a << endl; //将数值转化为string的第二种方法:C++方法 string cc = convertToString(1976); cout << cc << endl; //将string转化为数值的第一种方法:C++方法 string dd = "2006"; int p = convertFromString(dd) + 2; cout << p << endl; return 0; }
2008
二、string详解
1、string对象的定义和初始化:
string s1; //默认构造函数,s1为空串
string s2(s1); //将s2初始化为s1的一个副本
string s3(“value”); //将s3初始化为一个字符串字面值副本
string s4(n, ‘c’); //将s4初始化为字符‘c’的n个副本
在IDE中可以按F1,查看string的帮助手册。
(1)示例
#include <string> #include <iostream> using namespace std; int main(void) { string s1; string s2("abcdefghijkl"); cout << s2 << endl; basic_string<char> s3("xxx"); // 等价于string s3("xxx"); cout << s3 << endl; string s4("abcdefg", 4); //构造4个字符的字符串 cout << s4 << endl; string s5(s2, 2, 3); //从2的位置,构造3个字符的字符串 cout << s5 << endl; string::iterator first = s2.begin() + 1; string::iterator last = s2.begin() + 3; string s6(first, last); //[first, last) cout << s6 << endl; return 0; }运行结果:
2、常用成员函数:
(1)示例1
#include <string> #include <iostream> using namespace std; int main(void) { string s1("abcdefdg"); cout << s1.size() << endl; cout << s1.length() << endl; cout << s1.empty() << endl; cout << s1.substr(1, 2) << endl; cout << s1.substr(1) << endl; //等价于s1.substr(1, string::npos)或者s1.substr(1, -1); string::size_type pos = s1.find('d', 1);//第二个参数不填,默认位置从0开始算起 if (pos == string::npos) //npos = -1 cout << "not found" << endl; else cout << "pos=" << pos << endl; pos = s1.rfind('d'); //反向查找 if (pos == string::npos) cout << "not found" << endl; else cout << "pos=" << pos << endl; return 0; }运行结果:
(2)示例2
#include <string> #include <iostream> using namespace std; int main(void) { string s1("abcdefghijkl"); s1.replace(2, 2, "AAAAAA"); //从位置2替换2个字符为“AAAAAA”,替换的长度不限 cout << s1 << endl; s1 = "abcdefg"; s1.replace(s1.begin() + 1, s1.begin() + 4, "AAAAAA"); //从位置[1, 4)替换为字符串 cout << s1 << endl; string s2 = "xyzabc"; s2.insert(2, "MMMM"); //在位置2之前插入 cout << s2 << endl; s2.append("6666"); //追加字符 cout << s2 << endl; string s3 = "111"; s2.swap(s3); //跟另外一个字符串对象进行交换 cout << s2 << endl; cout << s3 << endl; return 0; }运行结果:
(3)示例3
#include <string> #include <iostream> using namespace std; void fun(char *str) { cout << str << endl; } int main(void) { string s1 = "abc"; s1[1] = 'B'; //重载[]运算符,返回s1的引用 cout << s1 << endl; const string s2 = "xyz"; //不能给常量赋值 //s2[1] = 'Y'; Error s2[1] 返回的是 const char& string s3 = "111" + s1 + "222" ; //string s3 = "111" + "222" + s1; //Error,前面两个中一定要有一个是对象 cout << s3 << endl; //s3.c_str(); fun(const_cast<char *>(s3.c_str())); //转换成 char * return 0; }运行结果:
(4)示例4
#include <string> #include <iostream> using namespace std; int main() { string strinfo = " //*---Hello World!......------"; string strset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; string::size_type first = strinfo.find_first_of(strset); //在strinfo中查找第一个在strset 字符串中出现的字符,即H if(first == string::npos) cout << "not find any characters" << endl; string::size_type last = strinfo.find_last_of(strset); //在strinfo中查找最后一个在strset 字符串中出现的字符,即d if(last == string::npos) cout << "not find any characters" << endl; cout << strinfo.substr(first, last - first + 1) << endl; //取子串 return 0; }运行结果:
(5)示例5
find_first_not_of,第一个不在字符串的位置
可以利用find_first_not_of等操作去除空格,如去除左空格可以这样:
string s = " afas";
string drop = " \t";
s.erase(0, s.find_first_not_of(drop));
去除右空格:
string s = " dsfs ";
string drop = " \t";
s.erase(s.find_last_not_of(drop)+1);