心胸决定格局,眼界决定境界...

[转]std::basic_string::substr

basic_string substr( size_type pos = 0, size_type count = npos );

字符串的pos位置作为子串的起始位置,npos为子串长度

位置从0开始

count == npos, the returned substring is [pos, size(),需判断

#include <string>
#include <iostream>

int main()
{
std::string a = "0123456789abcdefghij";

std::string sub1 = a.substr(10);
std::cout << sub1 << '\n';

std::string sub2 = a.substr(5, 3);
std::cout << sub2 << '\n';

std::string sub3 = a.substr(12, 100);
std::cout << sub3 << '\n';

 

 

string filepath("C:\\Documents and Settings\\Application Data\\123.wav");
int index1 = filepath.find_last_of("\.");
int index2 = filepath.find_last_of("\\");

if(string::npos != index1 && string::npos != index2 && index1 > index2)
{
string filetype = filepath.substr(index1 + 1);
string filename = filepath.substr(index2 + 1, index1 - index2-1);
cout << filetype << endl;
cout << filename << endl;
}

 


}

 

Output:

1 abcdefghij
2 567
3 cdefghij
4.wav
5.123
posted @ 2013-09-22 16:24  WELEN  阅读(661)  评论(0)    收藏  举报