C++中substr函数的用法
2017-05-22 14:24
399 查看
substr用法
basic_string substr( size_type pos = 0, size_type count = npos ) const;
Returns a substring [pos, pos+count). If the requested substring extends past the end of the string, or if count == npos, the returned substring is [pos, size()).
Parameters
pos - position of the first character to include count - length of the substring
Return value
String containing the substring [pos, pos+count).
Exceptions
std::out_of_range if pos > size()
Complexity
Linear in count
#include <string> #include <iostream> using namespace std; int main() { string a = "0123456789abcdefghij"; // count is npos, returns [pos, size()) //从位置十个开始取到末尾 string sub1 = a.substr(10); cout << sub1 << '\n'; //abcdefghij // 从位置5开始取,取三个 string sub2 = a.substr(5, 3); cout << sub2 << '\n'; //567 // 取最后三个 string sub4 = a.substr(a.size()-3, 50); cout << sub4 << '\n'; //hij try { // pos is out of bounds, throws //当所取位置超出了字符串长度,将抛出异常 string sub5 = a.substr(a.size()+3, 50); cout << sub5 << '\n'; } catch(const std::out_of_range& e) { cout << "pos exceeds string size\n"; } }
相关文章推荐
- C++中substr函数的用法
- C++中substr函数的用法
- C++中substr函数的用法
- C++中substr函数的用法
- C++中substr函数的用法
- C++的substr函数的用法
- C++中substr函数的用法
- C++中substr函数的用法
- C++中substr函数的用法
- C++中substr函数的用法
- C++中substr函数的用法
- C++中substr函数的用法
- oracle的substr函数的用法
- 关于C++中函数指针的使用(包含对typedef用法的讨论)
- 关于C++中函数指针的使用(包含对typedef用法的讨论)
- 关于C++中函数指针的使用(包含对typedef用法的讨论)
- 关于C的变长参数的思考心得/C/C++语言中可变参数的用法
- C++中 #define的用法
- C++中分层的用法
- c++ volatile用法(转)