C++之string常用方法总结

// 常用方法 ...
// http://www.cplusplus.com/reference/string/string
c_str
    Get C string equivalent (public member function )
data
    Get string data (public member function )
get_allocator
    Get allocator (public member function )
copy
    Copy sequence of characters from string (public member function )
find
    Find content in string (public member function )
rfind
    Find last occurrence of content in string (public member function )
find_first_of
    Find character in string (public member function )
find_last_of
    Find character in string from the end (public member function )
find_first_not_of
    Find absence of character in string (public member function )
find_last_not_of
    Find non-matching character in string from the end (public member function )
substr
    Generate substring (public member function )
compare
    Compare strings (public member function )
str.find(str1)
说明:从pos(默认是是0,即从头开始查找)开始查找,
找到第一个和str1相匹配的子串,**返回该子串的起始索引位置**
如果没有找到则返回string::npos
str.find_first_of(str1)
说明:从pos(默认是是0,即从头开始查找)开始查找,
找到第一个和str1相匹配的子串,**返回该子串的起始索引位置**
如果没有找到则返回string::npos
str.find_last_of(str1)
说明:从npos(默认是字符串最后一个,即从后向前查找)开始查找,
找到第一个和str1相匹配的子串,**返回该子串的最后一个字符的索引位置**
如果没有找到则返回string::npos
// 获取视频文件路径中的文件名 ...
string str = "/root/video/a.mp4"
unsigned int pos = str.find_last_of("/");
if (pos != string::npos)
    str.substr(pos + 1, str.size() - 1)
...
str.erase(pos); 
说明:删除str中下标pos到末尾的所有字符
str.erase(first,num)
说明:删除从first开始num个字符

#include<string>
#include<iostream>
using namespace std;

int main()
{
    string str = "0123456789";
    str.erase(7);
    cout << str  << endl;   //结果:0123456
    str.erase(0, 2);
    cout << str << endl;    // 结果:23456
}