字符串处理,要用string里面的函数

liujiaji 2010-07-16 05:14:03
比如:字符串1:c:\dir\file1.txt
字符串2:c:\dir\file2.txt

我想把这俩个文件合并,并生成新的文件名是:c:\dir\file1_file2.txt


一定要用string类。只求文件名生成的方法??

...全文
110 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
shihang_apo 2010-07-16
  • 打赏
  • 举报
回复
可以去这个链接看看。

http://read.newbooks.com.cn/info/119627.html

里面讲的很全。

另外求助:我怎么在MSDN里面没有找到关于string的介绍??
pengzhixi 2010-07-16
  • 打赏
  • 举报
回复
#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;
}
algorithms_memo 2010-07-16
  • 打赏
  • 举报
回复
C++ STL string:

#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;
}
liujiaji 2010-07-16
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 sunlp007 的回复:]

C/C++ code
#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……
[/Quote]

结了,晚一步
sunlp007 2010-07-16
  • 打赏
  • 举报
回复
#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;
}

编译通过
liujiaji 2010-07-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 phpjspasp 的回复:]

C/C++ code

#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_la……
[/Quote]

正解,多谢!
liutengfeigo 2010-07-16
  • 打赏
  • 举报
回复

学习了
phpjspasp 2010-07-16
  • 打赏
  • 举报
回复

#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;

}


这个吧。
phpjspasp 2010-07-16
  • 打赏
  • 举报
回复

#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;

}
selooloo 2010-07-16
  • 打赏
  • 举报
回复
先用字符串2 string的 rfind '\',然后find '.',这样就获得了file2的前后位置,将其拷贝出来
然后 字符串1的 rfind '\',然后insert 就可以了
liujiaji 2010-07-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhengweihit 的回复:]

对file1.txt字符串rfind()找到 . 出现的最后一个位置,replace()掉 .txt 为 _ ,将 file2.txt 字符串 append()到第一个处理完的文件名。
[/Quote]

谢谢,在整详细点啊,string不会
liujiaji 2010-07-16
  • 打赏
  • 举报
回复
用c方法我会,就是不会用string里面的

c的方法就是1.定义一个字符串,申请空间2.将第一个字符串(直到第一个.为止)拷贝到新增的字符串中

。。。。。。。。。

求string类方法?
sunlp007 2010-07-16
  • 打赏
  • 举报
回复
这个应该很简单,就用append函数
zhengweihit 2010-07-16
  • 打赏
  • 举报
回复
对file1.txt字符串rfind()找到 . 出现的最后一个位置,replace()掉 .txt 为 _ ,将 file2.txt 字符串 append()到第一个处理完的文件名。
  • 打赏
  • 举报
回复
先取前面共同的部分,然后做你相应操作,貌似跟函数没关系,看你怎么想了
liujiaji 2010-07-16
  • 打赏
  • 举报
回复
代码要是多的话,就跟我说说要用到的函数,或者写一个伪代码就行

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧