delphi清理txt文件多余的空格
PDF文件转存为文本,多了一堆不需要的空格,写个小程序处理一下,没逻辑,直接上代码。
delphi用的是XE11.3
unit UnitSmallMain; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Controls.Presentation, FMX.StdCtrls, FMX.Memo.Types, FMX.ScrollBox, FMX.Memo; type TForm4 = class(TForm) btn1: TSpeedButton; Memo1: TMemo; procedure btn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form4: TForm4; implementation {$R *.fmx} uses System.IOUtils; function GetAllFile(FilePath: string; const FileType: string): TStringList; var FDir, FFile: TSearchRec; I, J: Integer; AllPath: TStringList; begin AllPath := TStringList.Create; Result := TStringList.Create; try AllPath.Append(FilePath); repeat Application.ProcessMessages; FilePath := AllPath.Strings[0]; AllPath.Delete(0); I := FindFirst(FilePath + FileType, faAnyFile, FFile); while I = 0 do begin // Result.Append(FilePath + FFile.Name+','+ExtractFileExt(FFile.Name)+','+inttostr(FFile.Size)); Result.Append(FilePath + FFile.Name); //路径+文件名 //Result.Append(FFile.Name); //只获取文件名 I := FindNext(FFile); end; FindClose(FFile); J := FindFirst(FilePath + '*', faAnyFile, FDir); while J = 0 do begin if ((FDir.Attr and faDirectory) = faDirectory) and (FDir.Name <> '.') and (FDir.Name <> '..') then AllPath.Append(FilePath + FDir.Name + '\'); J := FindNext(FDir); end; FindClose(FDir); until AllPath.Count = 0; finally AllPath.Free; end; end; function AppPath: string; begin Result := TPath.GetLibraryPath; //(ExtractFilePath(Application.Name)); // TPath.GetDirectoryName(Application.GetNamePath); // ExtractFilePath(Application.ExeName); end; procedure TForm4.btn1Click(Sender: TObject); var sl: TStringList; I: Integer; sname, snewName: string; slTemp: TStringList; j: Integer; sb: TStringBuilder; begin try sl := GetAllFile(AppPath, '*.txt'); sb := TStringBuilder.Create; slTemp := TStringList.Create; try for I := 0 to sl.Count - 1 do begin sname := sl[I]; sb.Clear; slTemp.LoadFromFile(sname, TEncoding.UTF8); for j := 0 to slTemp.Count - 1 do begin sb.Append(TrimRight(slTemp[j])); end; slTemp.Text := sb.ToString; snewName := AppPath + 'new\' + TPath.GetFileNameWithoutExtension(sname) + '.txt'; slTemp.SaveToFile(snewName); Memo1.Lines.Add(snewName + '保存完成!'); end; except on e:Exception do begin Memo1.Lines.Add(e.Message); end; end; finally sb.Free; sl.Free; slTemp.Free; end; end; end.
窗体代码
object Form4: TForm4 Left = 810 Top = 330 Caption = 'Form4' ClientHeight = 480 ClientWidth = 640 Position = Designed FormFactor.Width = 320 FormFactor.Height = 480 FormFactor.Devices = [Desktop] Left = 810 Top = 330 DesignerMasterStyle = 0 object btn1: TSpeedButton Position.X = 480.000000000000000000 Position.Y = 288.000000000000000000 Text = 'btn1' OnClick = btn1Click end object Memo1: TMemo Touch.InteractiveGestures = [Pan, LongTap, DoubleTap] DataDetectorTypes = [] Position.X = 8.000000000000000000 Position.Y = 33.000000000000000000 Size.Width = 385.000000000000000000 Size.Height = 384.000000000000000000 Size.PlatformDefault = False TabOrder = 1 Viewport.Width = 381.000000000000000000 Viewport.Height = 380.000000000000000000 end end
文章来源于天涯神贴
之后
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步