64,648
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include<string>
using namespace std;
int main()
{
string s1="c:\\dir\\file1.txt:";
string s2="c:\\dir\\file2.txt:";
size_t pos1=s2.find_last_of('\\');
size_t pos2=s2.find_first_of('.');
size_t count=pos2-pos1-1;
size_t pos3=s1.find_first_of('.');
s1.insert(pos3,1,'_');
cout<<s1<<endl;
size_t pos4=s1.find_first_of('_');
s1.insert(pos4+1,s2,pos1+1,count);
cout<<s1<<endl;
system("pause");
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("c:\\dir\\file1.txt");
string s2("c:\\dir\\file2.txt");
string::size_type beg1, beg2, end1;
beg1 = s1.rfind('\\');
if (string::npos == beg1) beg1 = 0;
else ++beg1;
beg2 = s2.rfind('\\');
if (string::npos == beg1) beg1 = 0;
else ++beg2;
end1 = s1.rfind('.'); if (string::npos == end1) end1 = s1.length()-1;
string s(s1.substr(beg1,end1-beg1)+"_"+s2.substr(beg2));
cout<<"s = "<<s<<endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1="c:\\dir\\file1.txt",s2="c:\\dir\\file2.txt",s3;
s3.append(s1.begin(),s1.begin()+12);
s3+='_';
s3.append(s2.begin()+7,s2.begin+16);
cout<<s3<<endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "c:\\dir\\file1.txt";
string str2 = "c:\\dir\\file2.txt";
size_t found1 = str1.find_last_of(".");
size_t found2 = str2.find_last_of("\\");
str1 = str1.substr(0, found1) + "_" + str2.substr(found2+1);
cout<<str1<<endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "c:\\dir\\file1.txt";
string str2 = "c:\\dir\\file2.txt";
size_t found = str2.find_last_of("\\");
str1 = str1 + "_" + str2.substr(found+1);
cout<<str1<<endl;
return 0;
}