Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14490255
  • 博文数量: 5645
  • 博客积分: 9880
  • 博客等级: 中将
  • 技术积分: 68081
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-28 13:35
文章分类

全部博文(5645)

文章存档

2008年(5645)

我的朋友

分类:

2008-04-28 20:47:27

下载本文示例代码
p>  计算机世界的Web站点,越来越受到广大读者和网 友的喜爱,其中有一个重要原因,就是实用技巧栏目的推出。在实用技巧网页中,编辑对文 本文件进行了处理,在每个汉字中间增加一个空格,以便于网友在网上阅读,在因特网上也 有不少网站采取了此类便民措施。   我们从网上粘下这类文本文件之后,有时候需要 打印留存,但在打印时却遇到了麻烦,那就是汉字中的空格成为多余。以下Delphi程序利用Memo 控件作为过渡,可删除文件中的多余空格,重新存盘后,即可方便的用Word或Wps打印输出。   1、新建一个工程,在窗体上放置如下控件:Memo1、OpenDialog1 和3个按钮(“打开”、“存盘”、“删除多余空格”),设置Memo1的属性WordWrap=False,设置“存 盘”和“删除多余空格”的属性Enabled=False。   2、编写三个按钮的事件代码。 procedureTForm1.ToolButton1Click(Sender:TObject); begin//打开一个文本文件 ifopendialog1.Executethenbegin memo1.Lines.LoadFromFile(opendialog1.FileName); caption:=DeleteBlank[ opendialog1.FileName ]; ifmemo1.Lines.Count0thenbegin toolbutton2.Enabled:=True; toolbutton3.Enabled:=True; end elsebegin toolbutton2.Enabled:=False; toolbutton3.Enabled:=False; end; end; end; procedureTForm1.ToolButton2Click(Sender:TObject); begin//将Memo1的内容写入原来文件 screen.Cursor:=crHourGlass; memo1.Lines.SaveToFile(opendialog1.FileName); toolbutton2.Enabled:=False; screen.Cursor:=crDefault; end; procedureTForm1.ToolButton3Click(Sender:TObject); vari,j,linecount,strlen:integer; buffer1,buffer2:string; begin//删除Memo1中的多余空格 screen.Cursor:=crHourGlass; linecount:=memo1.Lines.Count; //总行数 fori:=0tolinecount-1dobegin buffer1:=memo1.Lines[i]; iflength(trim(buffer1))=0thencontinue; //处理空白行 buffer2:=; strlen:=length(buffer1); //行长 j:=1; while(jthencontinue; //不为空格,则处理下一个字符 iford(buffer1[j 1])>=$A1thenbegin //后一个为汉字,则删除这个空格 j:=j 1; continue; end; end; memo1. es[i]:=buffer2; end; toolbutton3.Enabled:=False; screen.Cursor:=crDefault; end;   3、本程序在PWin95/98,Delphi3下调试通过,由 于篇幅所限,Memo1中的显示不能自动换行,读者可自行完善。   [阿昊说:这种技巧用C来实现可能会简单些。另 外,我们只在汉字前加一个空格,后面没有。] p>  计算机世界的Web站点,越来越受到广大读者和网 友的喜爱,其中有一个重要原因,就是实用技巧栏目的推出。在实用技巧网页中,编辑对文 本文件进行了处理,在每个汉字中间增加一个空格,以便于网友在网上阅读,在因特网上也 有不少网站采取了此类便民措施。   我们从网上粘下这类文本文件之后,有时候需要 打印留存,但在打印时却遇到了麻烦,那就是汉字中的空格成为多余。以下Delphi程序利用Memo 控件作为过渡,可删除文件中的多余空格,重新存盘后,即可方便的用Word或Wps打印输出。   1、新建一个工程,在窗体上放置如下控件:Memo1、OpenDialog1 和3个按钮(“打开”、“存盘”、“删除多余空格”),设置Memo1的属性WordWrap=False,设置“存 盘”和“删除多余空格”的属性Enabled=False。   2、编写三个按钮的事件代码。 procedureTForm1.ToolButton1Click(Sender:TObject); begin//打开一个文本文件 ifopendialog1.Executethenbegin memo1.Lines.LoadFromFile(opendialog1.FileName); caption:=DeleteBlank[ opendialog1.FileName ]; ifmemo1.Lines.Count0thenbegin toolbutton2.Enabled:=True; toolbutton3.Enabled:=True; end elsebegin toolbutton2.Enabled:=False; toolbutton3.Enabled:=False; end; end; end; procedureTForm1.ToolButton2Click(Sender:TObject); begin//将Memo1的内容写入原来文件 screen.Cursor:=crHourGlass; memo1.Lines.SaveToFile(opendialog1.FileName); toolbutton2.Enabled:=False; screen.Cursor:=crDefault; end; procedureTForm1.ToolButton3Click(Sender:TObject); vari,j,linecount,strlen:integer; buffer1,buffer2:string; begin//删除Memo1中的多余空格 screen.Cursor:=crHourGlass; linecount:=memo1.Lines.Count; //总行数 fori:=0tolinecount-1dobegin buffer1:=memo1.Lines[i]; iflength(trim(buffer1))=0thencontinue; //处理空白行 buffer2:=; strlen:=length(buffer1); //行长 j:=1; while(jthencontinue; //不为空格,则处理下一个字符 iford(buffer1[j 1])>=$A1thenbegin //后一个为汉字,则删除这个空格 j:=j 1; continue; end; end; memo1. es[i]:=buffer2; end; toolbutton3.Enabled:=False; screen.Cursor:=crDefault; end;   3、本程序在PWin95/98,Delphi3下调试通过,由 于篇幅所限,Memo1中的显示不能自动换行,读者可自行完善。   [阿昊说:这种技巧用C来实现可能会简单些。另 外,我们只在汉字前加一个空格,后面没有。] 下载本文示例代码


删除文本文件中的多余空格删除文本文件中的多余空格删除文本文件中的多余空格删除文本文件中的多余空格删除文本文件中的多余空格删除文本文件中的多余空格删除文本文件中的多余空格删除文本文件中的多余空格删除文本文件中的多余空格删除文本文件中的多余空格删除文本文件中的多余空格删除文本文件中的多余空格删除文本文件中的多余空格删除文本文件中的多余空格删除文本文件中的多余空格
阅读(163) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~