没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串。
size_type size();//size()函数返回字符串中现在拥有的字符数。
size_type length();//length()函数返回字符串的长度. 这个数字应该和size()返回的数字相同.
void clear();//清空
bool empty();//如果字符串为空则empty()返回真(true),否则返回假(false).
const char *data();//data()函数返回指向自己的第一个字符的指针.
const char *c_str();//c_str()函数返回一个指向正规C字符串的指针, 内容与本字符串相同.
*/
void test5()
{??
?string first( "This comes first" );
?string second( "And this is second" );
?first.swap( second );
?cout << first << endl;//And this is second
?cout << second << endl;//This comes first
?string s("What we have here is a failure to communicate");
?string sub = s.substr(5);
?cout << sub << endl;//we have here is a failure to communicate
?cout<<"sub size:"< ??
?sub.clear();//<==>sub="";
?cout< }
//C++11
//转换成 int;float;double;long;long long;long double;unsigned long;unsigned long long;
//stoi(),stof(),stod(),stold(),stol(),stoll(),stoul(),stoull(),
void test6()
{???
?const char *str1 = " 42";
?const char *str2 = "3.14159";
?const char *str3 = "31337 with words";
?std::cout << "std::stoi(\"" << str1 << "\") is " << std::stoi(str1) << '\n';//42
?std::cout << "std::stoi(\"" << str2 << "\") is " << std::stoi(str2) << '\n';//3
?std::cout << "std::stoi(\"" << str3 << "\") is " << std::stoi(str3) << '\n';//31337
?std::cout << "std::stof(\"" << str1 << "\") is " << std::stof(str1,NULL) << '\n';//42
?std::cout << "std::stof(\"" << str2 << "\") is " << std::stof(str2) << '\n';//3.14159
?std::cout << "std::stof(\"" << str3 << "\") is " << std::stof(str3) << '\n';//31337
?std::cout << "std::stod(\"" << str1 << "\") is " << std::stod(str1) << '\n';//42
?std::cout << "std::stod(\"" << str2 << "\") is " << std::stod(str2,NULL) << '\n';//3.14159
?std::cout << "std::stod(\"" << str3 << "\") is " << std::stod(str3) << '\n';//31337
?
?std::cout << "std::stold(\"" << str1 << "\") is " << std::stold(str1) << '\n';//42
?std::cout << "std::stold(\"" << str2 << "\") is " << std::stold(str2) << '\n';//3.14159
?std::cout << "std::stold(\"" << str3 << "\") is " << std::stold(str3,NULL) << '\n';//31337
?
?string s1 = "-8246821";//十进制(默认)
?string s2 = "-0xffff"; //十六进制
?string s3 = "-020";? //八进制
?string s4 = "11111110";//是十进制,若要二进制则:stol(s4,NULL,2)
?//long? 就是 int的加长版,而不是float的加长版
?cout<<"stol("< ?cout<<"stol("< ?cout<<"stol("< ?cout<<"stol("<
?cout<<"stoul("< ?cout<<"stoul("< ?cout<<"stoul("< ?cout<<"stoul("<
?cout<<"stoll("< ?cout<<"stoll("< ?cout<<"stoll("< ?cout<<"stoll("<
?cout<<"stoull("< ?cout<<"stoull("< ?cout<<"stoull("< ?cout<<"stoull("< }
//C++11
//num to string/wstring :to_string(),to_wstring()
void test7()
{
?long double f=3.1415926;
?std::wstring wpi = L"pi is " + std::to_wstring(f);
?wstring wperfect = std::to_wstring(_ULonglong(1+2+4+7+14)) + L" is a perfect number";
?wcout << wpi << L'\n';//3.14159
?wcout << wperfect << L'\n';//28
?std::string pi = "pi is " + std::to_string(f);
?string perfect = std::to_string(long long(1+2+4+7+14)) + " is a perfect number";
?cout << pi << endl;//3.14159
?cout << perfect << endl;//28
}
void Test(char h)
{
?cout<<"press key===="< ?switch(h)
?{
?case '0':? test0();break;
?case '1':? test1();break;
?case '2':? test2();break;
?case '3':? test3();break;
?case '4':? test4();break;
?case '5':? test5();break;
?case '6':? test6();break;
?case '7':? test7();break;
?case 27:
?case 'q':exit(0);break;
?default:cout<<"default "< ?}
}
void main()
{
?while(1)
?{
??Test(getch());
?}
} 总结
字符串string的操作无非就是:与别串操作;字串查询;自身操作;
A.自身属性:字符串长度
B.与别的字符串进行操作(string1 与 string2 之间的关系):
? 1.比较大小
? 2.附加,交换
? 3.在string1的某个位置上插入string2的字串
? 4.string1的某些字符(用位置(index)个数(num)来确定)被string2的某些字符替换
? 5.拷贝字串给另一个字符串
C.查找字串:
1.从某个位置开始查询字串第一次或最后一次出现的位置
2.在string1的某个字串中,查询字串第一次或最后一次出现的位置
3.从某个位置开始查询,某个字符第一次或最后一次出现的位置
D.与自身操作:? 1.清空;? 2.获取或删除某个位置上的字符;? 3.获取某个位置上的字串
E.与其他类型之间的转换:
1.字符串通过地址,atoi(),atof,atol()来转换成数值
2.C++11:std::stoi(),stof(),stod(),stold(),stol(),stoll(),stoul(),stoull()
3.数值转换成string或宽字符串wstring:std::to_string(),std::to_wstring()