求一个折分字符串的算法,请帮帮忙! ( 积分: 100 )

  • 主题发起人 主题发起人 hlei
  • 开始时间 开始时间
H

hlei

Unregistered / Unconfirmed
GUEST, unregistred user!
例如<br>字符串是 '123|456|789<br>A,B,C: string;<br>要怎么写才能让 A是第一个'|'前的字符,B是两个'|'中间的字符,C是最后一个'|'后面的字符.也就是 A:= '123'; B:= '456'; C:= '789';<br>字符串可能会出现汉字所以不能用 LeftStr RightStr 这些函数
 
例如<br>字符串是 '123|456|789<br>A,B,C: string;<br>要怎么写才能让 A是第一个'|'前的字符,B是两个'|'中间的字符,C是最后一个'|'后面的字符.也就是 A:= '123'; B:= '456'; C:= '789';<br>字符串可能会出现汉字所以不能用 LeftStr RightStr 这些函数
 
//由于使用了LeftStr函数,所以要引用StrUtils<br>//s:源串, s1 标志字符串<br>function TForm1.GetSubstr(s,s1:string):Tstringlist;<br>var<br> &nbsp;sTemp:String;<br> &nbsp;n:Integer;<br>begin<br> &nbsp;Result:=TStringList.Create;<br> &nbsp;if s=''then exit;<br> &nbsp;sTemp:=s;<br> &nbsp;n:=pos(s1,s);<br> &nbsp;while n &gt;0 do<br> &nbsp;begin<br> &nbsp; &nbsp; Result.add(LeftStr(sTemp,n-1));<br> &nbsp; &nbsp; Delete(sTemp,1,n);<br> &nbsp; &nbsp; n:=pos(s1,sTemp); &nbsp; &nbsp;//重新赋值给n,继续循环<br> &nbsp;end;<br> &nbsp;Result.add(sTemp); &nbsp;//最末端的那个子字符串<br>end;
 
你也可以使用copy函数代替leftstr,copy在system单元中,不必引用。<br>Result.add(LeftStr(sTemp,n-1));
 
1. LeftBStr函数<br><br>var<br> &nbsp; s,a,b,c : string ;<br> &nbsp; i : integer ;<br>begin<br> &nbsp; s := '123|456|789' ;<br> &nbsp; i := Pos('|',s) ;<br> &nbsp; a := Copy(s,1,i-1);<br> &nbsp; delete(s,1,i);<br> &nbsp; i := Pos('|',s) ;<br> &nbsp; b := Copy(s,1,i-1);<br> &nbsp; c := Copy(s,i+1,Length(s)) ;<br>end;
 
edit1.text:=123|456|789;<br>A:=copy(edit1.text,1,3);<br>B:=copy(edit1.text,5,7);<br>c:=copy(edit1.text,9,Length(edit1.text));
 
简单,如下<br>var<br> &nbsp;i, flag, StrPos: Integer;<br> &nbsp;Str: string;<br>begin<br> &nbsp;Str := '123|456|789';<br> &nbsp;flag := 0;<br> &nbsp;for i := 1 to Length(Str) do<br> &nbsp;begin<br> &nbsp; &nbsp;if Str = '|' then <br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Inc(flag);<br> &nbsp; &nbsp; &nbsp;case flag of <br> &nbsp; &nbsp; &nbsp; &nbsp;1: A := Copy(Str, 1, i - 1);<br> &nbsp; &nbsp; &nbsp; &nbsp;2: B := Copy(Str, StrPos , i - StrPos);<br> &nbsp; &nbsp; &nbsp; &nbsp;3: C := Copy(Str, StrPos , i - StrPos);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;StrPos := i + 1;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;s,A,B,C:string;<br> &nbsp;i:integer;<br>begin<br> &nbsp;s:='123|456|789';<br> &nbsp;i:=pos('|',s);<br> &nbsp;A:=copy(s,1,i-1);<br> &nbsp;delete(s,1,i);<br> &nbsp;i:=pos('|',s);<br> &nbsp;B:=copy(s,1,i-1);<br> &nbsp;delete(s,1,i);<br> &nbsp;C:=copy(s,1,length(s)) ;<br>end;
 
{=================================================================<br> &nbsp;功 &nbsp;能: &nbsp;長字符串,分隔為數組<br> &nbsp;參 &nbsp;數: &nbsp;StrArray:字符串 &nbsp;StrNo:字符串中的標示符<br> &nbsp;返回值: &nbsp;TStringList<br> &nbsp;調用方法: GetArrayData('12*23*34*45','*')<br>=================================================================}<br>Function TForm1.GetArrayData(StrArray:String;StrNo:String):TStringList;<br>var<br> &nbsp;StrTmp,CopyStr:string;<br> &nbsp;PosI:Integer;<br> &nbsp;ResultList:TStringList;<br>begin<br> &nbsp;StrTmp:=StrArray + StrNo;<br> &nbsp;CopyStr:=StrArray + StrNo;<br> &nbsp;ResultList:=TStringList.Create;<br> &nbsp;while Length(StrTmp) &gt; 0 do<br> &nbsp;begin<br> &nbsp; &nbsp;PosI:=Pos(StrNo,StrTmp);<br> &nbsp; &nbsp;CopyStr:=Copy(StrTmp,1,PosI-1);<br> &nbsp; &nbsp;ResultList.Add(CopyStr);<br> &nbsp; &nbsp;Delete(StrTmp,1,PosI);<br> &nbsp;end;<br> &nbsp;Result:=ResultList;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;ListBox1.Items.Text:=GetArrayData('12|23|34|45','|').Text;<br>end;
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;s:string;<br> &nbsp;i,n:integer;<br>begin<br> &nbsp;s:=trim(edit1.Text)+'|' ;<br> &nbsp;n:=0;<br> &nbsp;i:=pos('|',s);<br> &nbsp;while i&gt;0 do<br> &nbsp;begin<br> &nbsp; &nbsp; &nbsp;inc(n);<br> &nbsp; &nbsp; &nbsp;case n of<br> &nbsp; &nbsp; &nbsp; &nbsp;1:edit2.Text :=copy(s,1,i-1);<br> &nbsp; &nbsp; &nbsp; &nbsp;2:edit3.Text :=copy(s,1,i-1);<br> &nbsp; &nbsp; &nbsp; &nbsp;3:edit4.Text :=copy(s,1,i-1);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;delete(s,1,i);<br> &nbsp; &nbsp; &nbsp;i:=pos('|',s);<br> &nbsp;end;<br>end;
 
结贴给分啦~~~
 
var<br> &nbsp;s,A,B,C: string;<br> &nbsp;i:integer;<br>begin<br>s:=edit1.Text;<br>i:=pos('|',s);<br>A:=copy(s,1,i-1);<br>S:=copy(s,i+1,length(s)-i);<br><br><br>i:=pos('|',s);<br>B:=copy(s,1,i-1);<br>S:=copy(s,i+1,length(s)-i);<br><br><br>i:=pos('|',s);<br>C:=copy(s,1,i-1);<br>S:=copy(s,i+1,length(s)-i);<br>end;
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;v_Str: TStrings;<br> &nbsp;v_Count: Integer;<br>begin<br> &nbsp;v_Str := TStringList.Create;<br> &nbsp;v_Str.Delimiter := '|';<br> &nbsp;v_Str.DelimitedText := '123|456|789';<br> &nbsp;for v_Count &nbsp;:= 0 to v_Str.Count -1 do<br> &nbsp; &nbsp;Memo1.Lines.Add(v_Str.Strings[v_Count]);<br> &nbsp;//Memo1.Lines := TStrings(v_Str);<br> &nbsp;v_Str.Free;<br>end;
 
我用了两年,没有问题的<br>之所以用WideString是因为处理汉字占两个字符的长度问题<br>分给我,我这里还有处理此类字符串的整套函数<br>function GetPartStr(S:WideString; iPosition:Integer;<br> &nbsp;sSeparator:WideString='|'):String;<br>var<br> &nbsp;i,iLength,iIndex,iCurrentPosition:Integer;<br>begin<br> &nbsp;Result:='';<br> &nbsp;if (RightStr(S,1)&lt;&gt;sSeparator) then S:=S+sSeparator;<br> &nbsp;iIndex:=1; iCurrentPosition:=1; iLength:=Length(S);<br> &nbsp;for i:=1 to iLength do<br> &nbsp; &nbsp;if (S=sSeparator) then<br> &nbsp; &nbsp; &nbsp;if (iCurrentPosition=iPosition) then begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Result:=Trim(System.Copy(S,iIndex,i-iIndex));<br> &nbsp; &nbsp; &nbsp; &nbsp;Break;<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp;else begin<br> &nbsp; &nbsp; &nbsp;Inc(iCurrentPosition);<br> &nbsp; &nbsp; &nbsp;iIndex:=i+1;<br> &nbsp; &nbsp;end;<br>end;
 
分给Avalon,我这还有一种:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;v_Str: TStrings;<br>begin<br> &nbsp;v_Str := TStringList.Create;<br> &nbsp;v_Str.Text := StringReplace('123|456|789','|',#13#10,[rfReplaceAll]);<br> &nbsp;Memo1.Lines := v_Str;<br> &nbsp;v_Str.Free;<br>end;
 
ExtractStrings(['|'], [], '123|456|789', Memo1.Lines);
 
楼上几位借助 TStrings 的方法如果字符串中包含回车换行就不能用了,要么先把回车换行替换处理,要么用另外的方法。我这里有一组专门处理这些字符串的函数供参考,其中字符串转成数组的有两个不同的,一个借助TStrings处理(对于包含回车换行的有问题),另一个直接处理字符串(效率低一点)<br><br>function StrExistsCount(S,Sub:string;CanRepeat:boolean;IsCaseSensitive:boolean):integer;<br>var i,j:integer;<br> &nbsp; &nbsp;s1:string;<br> &nbsp; &nbsp;len:integer;<br>begin &nbsp;//一个字符串在另一个字符串中出现的次数<br> &nbsp;Result:=0;<br> &nbsp;if length(s)&lt;length(sub) then exit;<br> &nbsp;j:=0;<br> &nbsp;len:=length(sub);<br> &nbsp;if IsCaseSensitive then<br> &nbsp; &nbsp;s1:=sub<br> &nbsp;else<br> &nbsp; &nbsp;s1:=LowerCase(sub);<br><br> &nbsp;if CanRepeat then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;for i:=1 to length(s)-len+1 do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if IsCaseSensitive then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if copy(s,i,len)=s1 then inc(j)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if LowerCase(copy(s,i,len))=s1 then inc(j)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end<br> &nbsp;else<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;i:=1;<br> &nbsp; &nbsp; &nbsp;while i&lt;=length(s)-len+1 do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if IsCaseSensitive then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if copy(s,i,len)=s1 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inc(j);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i:=i+len;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Continue;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if LowerCase(copy(s,i,len))=s1 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inc(j);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i:=i+len;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Continue;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;inc(i); &nbsp;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp;Result:=j;<br>end;<br><br>function DimLBound(d:Variant):integer;<br>begin &nbsp;//数组下限<br> &nbsp;if VarIsArray(d) then<br> &nbsp; &nbsp;Result:=VarArrayLowBound(d,1)<br> &nbsp;else<br> &nbsp; &nbsp;Result:=-1;<br>end;<br><br>function DimHBound(d:Variant):integer;<br>begin &nbsp;//数祖上限<br> &nbsp;if VarIsArray(d) then<br> &nbsp; &nbsp;Result:=VarArrayHighBound(d,1)<br> &nbsp;else<br> &nbsp; &nbsp;Result:=-1; &nbsp;<br>end;<br><br><br>function IsDim(d:Variant):boolean;<br>begin &nbsp;//判断是否为数据变量<br> &nbsp;Result:=VarIsArray(d);<br>end;<br><br>function DimCount(d:Variant):integer;<br>begin &nbsp;//数组长度<br> &nbsp;if VarIsArray(d) then<br> &nbsp; &nbsp;Result:=DimHBound(d)-DimLBound(d)+1<br> else<br> &nbsp; &nbsp;Result:=-1;<br>end;<br><br>function DimToStr(d:Variant;Separator:string):string;<br>var i:integer;<br> &nbsp; &nbsp;s:string;<br>begin &nbsp;//数组合成字符串<br> &nbsp;s:='';<br> &nbsp;if VarIsArray(d) then<br> &nbsp; &nbsp;for i:=DimLBound(d) to DimHBound(d) do<br> &nbsp; &nbsp; &nbsp;s:=s+String(d)+Separator;<br> &nbsp;if s&lt;&gt;'' then delete(s,length(s)-length(Separator)+1,length(Separator));<br> &nbsp;Result:=s;<br>end;<br><br>function StrToDim(S,Separator:string):Variant;<br>var d:Variant;<br> &nbsp; &nbsp;i,l:integer;<br> &nbsp; &nbsp;StrList:TStrings;<br>begin &nbsp;//字符串转换为数(组若内容包含#13,则数据不正确)<br> &nbsp;Result:=Null;<br> &nbsp;if s='' then exit;<br> &nbsp;s:=StringReplace(s,Separator,#13#10,[rfReplaceAll,rfIgnoreCase]);<br> &nbsp;StrList:=TStringList.Create;<br> &nbsp;try<br> &nbsp; &nbsp;StrList.Text:=s;<br> &nbsp; &nbsp;l:=StrList.Count-1;<br> &nbsp; &nbsp;d:=VarArrayCreate([0,l],varVariant);<br> &nbsp; &nbsp;i:=0;<br> &nbsp; &nbsp;while i&lt;=l do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;d:=StrList.Strings;<br> &nbsp; &nbsp; &nbsp;inc(i);<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;FreeAndNil(StrList);<br> &nbsp;end;<br> &nbsp;Result:=d;<br>end;<br><br>function StrToDim(S,Separator:string):Variant;<br>var d:Variant;<br> &nbsp; &nbsp;i,j,k,len,len1,l:integer;<br>begin &nbsp;//字符串转换为数组(效率不高,代码可读性差)<br> &nbsp;Result:=Null;<br> &nbsp;if s='' then exit;<br> &nbsp;s:=s+separator;<br> &nbsp;l:=StrExistsCount(s,separator,false,false);<br> &nbsp;separator:=LowerCase(separator);<br> &nbsp;len:=length(s);<br> &nbsp;len1:=length(Separator);<br> &nbsp;k:=0;<br> &nbsp;d:=VarArrayCreate([0,l-1],varVariant);<br> &nbsp;i:=1;<br> &nbsp;while i&lt;=len-len1 do<br> &nbsp;begin<br> &nbsp; &nbsp;for j:=i to len do<br> &nbsp; &nbsp;if LowerCase(Copy(s,j,len1))=separator then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if k&lt;=l-1 then d[k]:=copy(s,i,j-i);<br> &nbsp; &nbsp; &nbsp;inc(k);<br> &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;i:=j+len1;<br> &nbsp;end;<br> &nbsp;Result:=d;<br>end;<br><br><br>--------------------------------------<br>var v:Variant;<br> &nbsp; &nbsp;s:string;<br>begin<br> &nbsp;s:='12,23,12,234,12234,231'<br> &nbsp;v:=StrToDim(s,',');<br> &nbsp;v[0]:=……<br> &nbsp;v[1]:=……<br>end;
 
多人接受答案了。
 
后退
顶部