字符串的问题
假定给一个string类字符串,要想节取中间的一段该怎么弄?谢谢了
程序代码:
#include <iostream> #include <string> using namespace std; bool get_substr(const string & str,int pos/*截取位置,0开始*/,unsigned int length/*截取长度*/,char ** lpsubstr/*out*/) { if(pos >= str.length()) return false; if(pos + length >= str.length()) length = str.length() - pos; *lpsubstr = new char[length+1]; if(!*lpsubstr) return false; memset(*lpsubstr,0,length+1); memcpy(*lpsubstr,&str.c_str()[pos],length); return true; } int main() { string str("1234567890"); char * psubstr = NULL; if(get_substr(str,7,5,&psubstr) && psubstr) { cout <<psubstr<<endl; delete[] psubstr; psubstr = NULL; } system("pause"); return 0; }