关于删除多余回车的问题(100分)

  • 主题发起人 主题发起人 dedragon
  • 开始时间 开始时间
D

dedragon

Unregistered / Unconfirmed
GUEST, unregistred user!
我想实现的功能如下:有个button和RichEdit,在RichEdit输入或者粘贴一篇文字,文字中带有很多多余的回车键。判断的正确回车的标准是以中文的段落标点符号。按button后就可以自动的把多余的回车键删除。并且有多个段落的话,也按照中文的格式,段落行前留两个空格再书写文字。<br>如下面一段文字:<br>&nbsp; &nbsp; 13131313131331313<br>&nbsp; &nbsp; 1313131313131313212544485;<br>&nbsp; &nbsp; 4798778784。大幅度法,大幅度法,大幅度法地方的热。<br>&nbsp; &nbsp; 反对反对。<br>整理之后输出为:<br>&nbsp; &nbsp; 131313131313313131313131313131313212544485;4798778784。大幅度法,大幅度法,大幅度法地方的热。<br>&nbsp; &nbsp; 反对反对。<br><br>原来有个vb程序的了,我根据vb程序改成delphi如下:<br>procedure TForm1.Button3Click(Sender: TObject);<br>var<br>&nbsp; I,j,Strlen,linecount:integer; &nbsp; //J为当前位置,Strlen为文本长度<br>&nbsp; Content,temp,temp1,buffer,buffer1:string; &nbsp;//Content为文本内容,temp为临时字符,buffer为原有字符<br>begin<br>&nbsp; linecount:=RichEdit1.Lines.count;<br>&nbsp; for I:=0 to linecount-1 do &nbsp; begin<br>&nbsp; buffer1:=RichEdit1.Lines; &nbsp;<br>&nbsp; Content:=Trim(buffer1);<br>&nbsp; Content:=' &nbsp; &nbsp;'+ Content;<br>&nbsp; strlen:=length(buffer1);<br>&nbsp;// showmessage(inttostr(strlen));<br>// &nbsp;showmessage(buffer1);<br>&nbsp; for I=3 to strlen-1 do begin<br>&nbsp; &nbsp; buffer:=Midstr(Content, I, 1);<br>&nbsp; &nbsp; if buffer=Chr(13) then<br>&nbsp; &nbsp; &nbsp; temp:=Midstr(Content, I+1, 1);<br>&nbsp; &nbsp; &nbsp; if ord(temp)=10 then<br>&nbsp; &nbsp; &nbsp; &nbsp; temp:=Midstr(Content, I-1, 1);<br>&nbsp; &nbsp; &nbsp; &nbsp; if temp=Chr(10) or temp=' ' or (temp&lt;&gt;Chr(34) And temp&lt;&gt;Chr(39) And AnsiMatchStr(temp, '。”!:?.!:?')=0) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Content:=Midstr(Content, 1, I-1) + Midstr(Content, I+2, strlen-1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strlen:=strlen-2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; I:=I-1<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Content:=Midstr(Content, 1, I+1) + '  ' + Midstr(Content, I+2, strlen-1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strlen:=strlen+2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; I:=I+3;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; I:=I+1;<br>&nbsp; Content:=Trim(Content);<br>&nbsp; if LeftStr(RightStr(Content,2),1) = Chr(13) then<br>&nbsp; &nbsp; Content:=Trim(LeftStr(Content, Length(Content) - 2));<br>&nbsp; end;<br>&nbsp; RichEdit1.Lines:=Content;<br>&nbsp; end; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>end;<br><br>但编译不通,请朋友们多多帮忙,或者给我个新的程序也可。
 
是否可以利用StrUtils单元的:<br>function AnsiReplaceStr(const AText, AFromText, AToText: string): string;<br>或者<br>function AnsiReplaceText(const AText, AFromText, AToText: string): string;<br>将回车替换成空字符串''或者其它什么的:)<br><br>
 
我是要删除多余的回车,并不是把所有的回车都去掉或者改成什么符号。<br>你说的这个我在之前的帖子也看到过了,并不是同一个意思的。<br>例如。”!:?.!:?这些符号后面有回车的话,就表示是正确的更换段落的标志,则不用删除,并把后面的文字继续按这种方法处理。
 
try this code:<br><br>uses StrUtils;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; I: Integer;<br>&nbsp; S, Line: string;<br>begin<br>&nbsp; S := '';<br>&nbsp; for I := 0 to Memo1.Lines.Count - 1 do<br>&nbsp; begin<br>&nbsp; &nbsp; Line := Trim(Memo1.Lines);<br>&nbsp; &nbsp; if AnsiSameText(LeftStr(Line, 2), ' '{chinese space}) then<br>&nbsp; &nbsp; &nbsp; Line := Copy(Line, 3, MaxInt);<br>&nbsp; &nbsp; if AnsiSameText(LeftStr(Line, 2), ' '{chinese space}) then<br>&nbsp; &nbsp; &nbsp; Line := Copy(Line, 3, MaxInt);<br>&nbsp; &nbsp; if Line = '' then Continue;<br>&nbsp; &nbsp; S := S + Line;<br>&nbsp; &nbsp; if (AnsiPos(RightStr(Line, 2), '。”!:?') &gt; 0) or<br>&nbsp; &nbsp; &nbsp; &nbsp;(AnsiPos(RightStr(Line, 1), '.!:?') &gt; 0) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; S := S + #13#10;<br>&nbsp; &nbsp; &nbsp; if I &lt;&gt; Memo1.Lines.Count - 1 then<br>&nbsp; &nbsp; &nbsp; &nbsp; S := S + '  '{2 chinese spaces};<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; Memo2.Lines.Text := S;<br>end;
 
我看了你写的程序,你的Pascal语法不太熟啊<br>其中用的字符串处理函数MidStr没有吧,应该用Copy<br>LeftStr、RightStr这些函数都没有,也得换成Copy<br>还有For循环中的循环变量不能用程序改变,还有你的两个嵌套循环怎么用一个循环变量<br>还有AnsiMatchStr(temp, '。”!:?.!:?')=0是完成什么功能的,我在VB中也没找到<br>AnsiMatchStr(temp, '。”!:?.!:?')这句中的temp你只取了一个字符,却要和'!'做比较,'!'是两个字符,这个可以自己写一个函数处理<br>如果真的想完成这个完整功能的话,写清楚一点,你根据什么来确定是一个段落呢(确定是一个段落的条件?)。<br>Email :q82s11c21@163.com
 
你们是真逗啊,Delphi中有自带的LeftStr函数<br>摘自lichengbin大虾,我改了一点<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp;I: Integer;<br>&nbsp;S, Line,tmp: string;<br>begin<br>&nbsp;S := '';<br>&nbsp;for I := 0 to Memo1.Lines.Count - 1 do<br>&nbsp;begin<br>&nbsp; &nbsp;Line := Trim(Memo1.Lines);<br>&nbsp; &nbsp;if AnsiSameText(Copy(Line,1, 2), ' '{chinese space}) then<br>&nbsp; &nbsp; &nbsp;Line := Copy(Line, 3, MaxInt);<br>&nbsp; &nbsp;if AnsiSameText(Copy(Line,1, 2), ' '{chinese space}) then<br>&nbsp; &nbsp; &nbsp;Line := Copy(Line, 3, MaxInt);<br>&nbsp; &nbsp;if Line = '' then Continue;<br>&nbsp; &nbsp;S := S + Line;<br>&nbsp; &nbsp;tmp :=Copy(Line,Length(Line)-1,2);<br>&nbsp; &nbsp;if ((tmp= '。')or(tmp ='”')or(tmp='!')or(tmp=':')or(tmp='?')) or<br>&nbsp; &nbsp; &nbsp; (Line[Length(Line)] in [ '.','!',':','?']) then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;S := S + #13#10;<br>&nbsp; &nbsp; &nbsp;if I &lt;&gt; Memo1.Lines.Count - 1 then<br>&nbsp; &nbsp; &nbsp; &nbsp;S := S + '  '{2 chinese spaces};<br>&nbsp; &nbsp;end;<br>&nbsp;end;<br>&nbsp;Memo2.Lines.Text := S;<br>end; &nbsp;<br>
 
我又看了一下,改的也不对,和你原来的程序不一样了,好像缺点什么,我再看看
 
to lichengbin:<br>&nbsp; &nbsp; 您的程序我测试过,基本上是完成了我所说的内容,但第一行前还没实现空两格,其他特殊情况还没试过。<br>to 仙剑奇侠:<br>&nbsp; &nbsp; 不好意思,AnsiMatchStr是VB里的函数,是判断一个字符是否与某串字符中的某一个想匹配的。我想改成delphi的,但找不到同一功能的函数,忘了注释出来。另外我认为判断为段落的根据就是在。”!:?.!:?这几种符号后面加上回车键就能判断这是一段的结束了,然后另起一段空两个格开始书写后面的文字。
 
等中午吃过饭我就给你写出来
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; I: Integer;<br>&nbsp; S, Line: string;<br>begin<br>&nbsp; S := '  '{2 chinese spaces};<br>&nbsp; for I := 0 to Memo1.Lines.Count - 1 do<br>&nbsp; begin<br>&nbsp; &nbsp; ....<br>&nbsp; end;<br>&nbsp; if Length(S) = 4 then S := '';<br>&nbsp; Memo2.Lines.Text := S;<br>end;
 
var<br>&nbsp; s,t :AnsiString;<br>&nbsp; tmpStr,tmp :string;<br>&nbsp; Ch :Char;<br>&nbsp; i :Integer;<br>begin<br>&nbsp; s :=RichEdit1.Text;<br>&nbsp; t :='';<br>&nbsp; while AnsiPos(#13,s) &gt;0 do<br>&nbsp; begin<br>&nbsp; &nbsp; i :=AnsiPos(#13,s);<br>&nbsp; &nbsp; tmpStr :=Copy(s,1,i);<br>&nbsp; &nbsp; Delete(s,1,i);<br>&nbsp; &nbsp; tmp :=Copy(tmpStr,Length(tmpStr)-2,2);<br>&nbsp; &nbsp; if Length(tmp) &gt;=2 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Ch :=tmp[2];<br>&nbsp; &nbsp; &nbsp; if ((tmp= '。')or(tmp ='”')or(tmp='!')or(tmp=':')or(tmp='?'))<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; or(ch in in [ '.','!',':','?']) then<br>&nbsp; &nbsp; &nbsp; &nbsp; t :=t+tmpStr+'  '{2 chinese space}<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; t :=t+Copy(tmpStr,1,i-1);<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; t :=t+Copy(tmpStr,1,i-1);<br>&nbsp; &nbsp; if Length(s) &gt; 0 then<br>&nbsp; &nbsp; &nbsp; if s[1] = #10 then<br>&nbsp; &nbsp; &nbsp; &nbsp; Delete(s,1,1);<br>&nbsp; end;<br>&nbsp; RichEdit2.Text :=t;<br>end;
 
谢谢各位的回答,仙剑奇侠和lichengbin朋友的都能实现我所说的功能~
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
922
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部