怎样在串中快速的取得第N到第N+1个*之间的字符子串----在线等待 (5分)

  • 主题发起人 主题发起人 terrace
  • 开始时间 开始时间
T

terrace

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个string类型的变量,长度约几千,其值中包含多个'*',如:as11*aaa*qq*dff*zzz*5*,怎样快速的取得第N到第N+1个*之间的字符串呢?
 
自己写代码吧,又不是很困难的事情.当然自己写的代码最快了
 
用POS函数,然后写一段代码就行了
 
var <br>&nbsp; s:string//假设s就是你说的那个包含多个*的字符串<br>&nbsp; str:string//就是你要求的字符串 <br>&nbsp;i:integer;<br>&nbsp; k:integer;假设k为出现第N个*出现的位置<br>begin<br>&nbsp; &nbsp;str:=''; <br>&nbsp; &nbsp;for &nbsp; i:=1 &nbsp; to &nbsp; length(s) &nbsp; &nbsp;do <br>&nbsp; &nbsp; &nbsp; &nbsp;if &nbsp;copy(s,i,1)='*' &nbsp; then &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k:=i;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;s:=copy(s,k+1,length(s)-k);<br>&nbsp; &nbsp; &nbsp;str:=copy(s,1,pos('*',s)-1);<br>&nbsp; end; <br>&nbsp; &nbsp; &nbsp;
 
用下面的函数,把这个字符串转化成一个数组,然后直接按照数组下标进行读取就可以了<br>&nbsp; TWords = array of String;<br><br>function SplitWithString(const S: String; SepStr: String): TWords;<br><br>implementation<br><br>function FindStr(const S, SX: String; var iStart, iEnd: Integer): Integer;<br>var<br>&nbsp; iMatch: Integer;<br>&nbsp; Len: Integer;<br>begin<br>&nbsp; Result := 0;<br>&nbsp; iMatch := 1;<br>&nbsp; Len := Length(SX);<br>&nbsp; while iStart &lt;= iEnd do<br>&nbsp; begin<br>&nbsp; &nbsp; if S[iStart] = SX[iMatch] then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; iMatch := iMatch + 1;<br>&nbsp; &nbsp; &nbsp; if iMatch &gt; Len then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; iStart := iStart + 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := iStart - iMatch + 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; Break;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else if iMatch &gt; 1 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; iStart := iStart - iMatch + 1;<br>&nbsp; &nbsp; &nbsp; iMatch := 1;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; iStart := iStart + 1;<br>&nbsp; end;<br>end;<br><br><br>function SplitWithString(const S: String; SepStr: String): TWords;<br>var<br>&nbsp; i, Len: Integer;<br>&nbsp; m, n: Integer;<br>&nbsp; ct: Integer;<br>begin<br>&nbsp; Len := Length(S);<br>&nbsp; i := 1;<br>&nbsp; ct := 0;<br>&nbsp; while i &lt;= Len do<br>&nbsp; begin<br>&nbsp; &nbsp; m := i;<br>&nbsp; &nbsp; n := FindStr(S, SepStr, i, Len);<br>&nbsp; &nbsp; if n = 0 then n := Len + 1;<br>&nbsp; &nbsp; if n &gt; m then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; SetLength(Result, ((ct + 10) div 10) * 10);<br>&nbsp; &nbsp; &nbsp; Result[ct] := Copy(S, m, n - m);<br>&nbsp; &nbsp; &nbsp; ct := ct + 1;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; SetLength(Result, ct);<br>end;<br>
 
当然还有更简单的方法,<br>把 * 替换为 #13#10 然后赋值给 TStrings.Text<br><br>var<br>&nbsp; s: String;<br>&nbsp; st: TStringList;<br>&nbsp; i: Integer;<br>begin<br>&nbsp; s := 'as11*aaa*qq*dff*zzz*5*';<br>&nbsp; st := TStringList.Create;<br>&nbsp; st.Text := StringReplace(s, '*', #13#10, [rfReplaceAll]);<br>&nbsp; Result := st[N];<br><br>&nbsp;
 
后退
顶部