对string文件名路径中获取文件名函数方法

来源:互联网 发布:天庭淘宝店 宁小北txt 编辑:程序博客网 时间:2024/05/16 14:11
string getFilename(string s) {char sep = '/';char sepExt='.';#ifdef _WIN32sep = '\\';#endifsize_t i = s.rfind(sep, s.length( )); //rfind is a function which is finding sep from the end of the stringif (i != string::npos) //if there is nothing return back,return string::npos{string fn= (s.substr(i+1, s.length( ) - i));size_t j = fn.rfind(sepExt, fn.length( ));if (i != string::npos) {return fn.substr(0,j);}else{return fn;}}else{return "";}}

0 0