用for if str[i]='/' then str[i]:=','; if str[i]='{' then str[i]:=',' if str[i]='}' then str[i]:=','
----------------------------------------------
链接:https://pan.baidu.com/s/12jzmECYKhGCsHBxz8tmB6w 提取码:pelr --来自百度网盘超级会员V9的分享
function mySplit(const S: string): string; const Separator = ['{', '/', '}']; var len: Integer; i: Integer; m: Integer; begin Result := S;
len := Length(S); m := 1; for i := 1 to len do begin if CharInSet(S[i], Separator) then begin if not CharInSet(Result[m], Separator) and (i < len) and not CharInSet(S[i +1], Separator) then begin Result[m] := ','; Inc(m); end; end else begin Result[m] := S[i]; Inc(m); end; end; if m <= len then begin SetLength(Result, m -1); end; end;
可以使用正则来做。 for aMatch in TRegEx.Matches('{AA/BB/CC/DD}AB CD{11}{22}DEFG 0123{111/222/333/444}00000', '\b[\w ]+\b') do Memo1.Lines.Add(aMatch.Value);
----------------------------------------------
-
var I:Integer; S:string; A:TArray<string>; begin Memo1.Lines.Clear; S:='{AA/BB/CC/DD}AB CD{11}{22}DEFG 0123{111/222/333/444}00000'; A:=S.Split(['{','}','/',' '],TStringSplitOptions.ExcludeEmpty); for I := Low(A) to High(A) do Memo1.Lines.Add(A[I]);
----------------------------------------------
-
不知道你用什么版本的Delphi如果是高版本的,内置了字符串的拆分方法 var s, item : string; ss : TArray<string>; begin s := '{AA/BB/CC/DD}AB CD{11}{22}DEF'; ss := s.Split(['{','}','/'], TStringSplitOptions.ExcludeEmpty); for item in ss do begin ShowMessage(item); end; end;
----------------------------------------------
武稀松http://www.raysoftware.cn
function OpStr(AStr:string):string; var i,k:Integer; s:string; begin s:=''; k:=0; for i := 1 to Length(AStr) do begin if AStr[i] in ['{','/','}'] then begin if k=0 then //忽略首部特殊字符 Continue;
if s[k]=',' then //特殊字符相连 Continue;
s:=s+','; Inc(k); end else begin s:=s+AStr[i]; Inc(k); end; end;
if s[Length(s)]=',' then //忽略尾部特殊字符 Delete(s,Length(s),1);