64,654
社区成员
发帖
与我相关
我的任务
分享
#include <string>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
string sTest="D:\\Type\\family.txt";
string fe=sTest.substr(sTest.rfind("\\")+1);
cout << fe << endl; //family.txt
string f=fe.substr(0,fe.rfind("."));
cout << f << endl; //family
char fname[_MAX_FNAME];
_splitpath(sTest.c_str(),NULL,NULL,fname,NULL);
printf("%s\n",fname); //family
return 0;
}
#include <string>
#include <iostream>
using namespace std;
int main() {
string sTest="D:\\Type\\family.txt";
string fe=sTest.substr(sTest.rfind("\\")+1);
cout << fe << endl; //family.txt
string f=fe.substr(0,fe.rfind("."));
cout << f << endl; //family
return 0;
}
string s = sTest.substr(b+1,a-b-1);//ms应该这样
string sTest = "D:\\Type\\family.txt";
int a = sTest.find_last_of(".");
int b = sTest.find_last_of("\\");
string s = sTest.substr(b+1,a);
size_t pos=find(0,".");
然后从pos往前找一个'\',就行了.
// string::find_last_of
#include <iostream>
#include <string>
using namespace std;
void SplitFilename (const string& str)
{
size_t found;
cout << "Splitting: " << str << endl;
found=str.find_last_of("/\\");
cout << " folder: " << str.substr(0,found) << endl;
cout << " file: " << str.substr(found+1) << endl;
}
int main ()
{
string str1 ("/usr/bin/man");
string str2 ("c:\\windows\\winhelp.exe");
SplitFilename (str1);
SplitFilename (str2);
return 0;
}