下面是一段的VB代码,可做参照 GetValue("1.abc,2.def,3.ghi,4.jmk",4,",") //等于4.jmk //---------------------------------------------------------- Private Function GetValue(ByVal Src As String, ByVal Pos As Integer, ByVal Split As String) As String Dim PPos On Error GoTo ErrHandle If Src = "" Then GetValue = "" Exit Function End If PPos = InStr(Src, Split) Select Case Pos Case -1 If PPos > 0 Then GetValue = GetValue(Mid(Src, PPos + 1), Pos, Split) Else GetValue = Src End If Case 1 If PPos > 0 Then GetValue = Mid(Src, 1, PPos - 1) Else GetValue = Src End If Case Else GetValue = GetValue(Mid(Src, PPos + 1), Pos - 1, Split) End Select Exit Function ErrHandle: iec.ErrorHandle "CH27_OpenWinF.GetValue", Err End Function
procedure SpliteStr(strTmp:String;listTmp:TStringList); var i:Integer; iCount:Integer; begin iCount:=0; for i:=1 to Length(strTmp) do begin if strTmp[i]=',' then iCount:=iCount+1; end; for i:=1 to iCount do begin listTmp.Add(Copy(strTmp,1,Pos(',',strTmp)-1)); strTmp:=Copy(strTmp,Pos(',',strTmp)+1,Length(strTmp)); end; end;
outer2000(天外流星) 先感谢你,不过你说的方法不对呀
type TResultArray = array of string;function SplitString(const source, ch: string): TResultArray; var temp: string; i: integer; begin temp := source; i := pos(ch, source); while i <> 0 do begin SetLength(Result, Length(Result) + 1); Result[Length(Result) - 1] := copy(temp, 0, i - 1); delete(temp, 1, i); i := pos(ch, temp); end; SetLength(Result, Length(Result) + 1); Result[Length(Result)-1] := Temp; end; ************** function SplitString(const source,ch:string):tstringlist; var temp:string; i:integer; begin result:=tstringlist.Create; temp:=source; i:=pos(ch,source); while i<>0 do begin result.Add(copy(temp,0,i-1)); delete(temp,1,i); i:=pos(ch,temp); end; result.Add(temp); end; 调用: s:=splitstring('afsdfsdaaa|bbfdsfsdb|ccc','|'); for i:=0 to s.Count-1 do b:=b+s.Strings[i]+#13; showmessage(b); s.free;
var strstemp:tstringlist; i:integer; begin strstemp:=tstringlist.Create ; strstemp.Delimiter :=','; strstemp.DelimitedText:=s; for i:=0 to s.count-1 do showmessage(s[i]); strstemp.free end;
GetValue("1.abc,2.def,3.ghi,4.jmk",4,",") //等于4.jmk
//----------------------------------------------------------
Private Function GetValue(ByVal Src As String, ByVal Pos As Integer, ByVal Split As String) As String
Dim PPos
On Error GoTo ErrHandle
If Src = "" Then
GetValue = ""
Exit Function
End If
PPos = InStr(Src, Split)
Select Case Pos
Case -1
If PPos > 0 Then
GetValue = GetValue(Mid(Src, PPos + 1), Pos, Split)
Else
GetValue = Src
End If
Case 1
If PPos > 0 Then
GetValue = Mid(Src, 1, PPos - 1)
Else
GetValue = Src
End If
Case Else
GetValue = GetValue(Mid(Src, PPos + 1), Pos - 1, Split)
End Select
Exit Function
ErrHandle:
iec.ErrorHandle "CH27_OpenWinF.GetValue", Err
End Function
var
i:Integer;
iCount:Integer;
begin
iCount:=0;
for i:=1 to Length(strTmp) do
begin
if strTmp[i]=',' then
iCount:=iCount+1;
end;
for i:=1 to iCount do
begin
listTmp.Add(Copy(strTmp,1,Pos(',',strTmp)-1));
strTmp:=Copy(strTmp,Pos(',',strTmp)+1,Length(strTmp));
end;
end;
TResultArray = array of string;function SplitString(const source, ch: string): TResultArray;
var
temp: string;
i: integer;
begin
temp := source;
i := pos(ch, source);
while i <> 0 do
begin
SetLength(Result, Length(Result) + 1);
Result[Length(Result) - 1] := copy(temp, 0, i - 1);
delete(temp, 1, i);
i := pos(ch, temp);
end;
SetLength(Result, Length(Result) + 1);
Result[Length(Result)-1] := Temp;
end;
**************
function SplitString(const source,ch:string):tstringlist;
var
temp:string;
i:integer;
begin
result:=tstringlist.Create;
temp:=source;
i:=pos(ch,source);
while i<>0 do
begin
result.Add(copy(temp,0,i-1));
delete(temp,1,i);
i:=pos(ch,temp);
end;
result.Add(temp);
end;
调用:
s:=splitstring('afsdfsdaaa|bbfdsfsdb|ccc','|');
for i:=0 to s.Count-1 do
b:=b+s.Strings[i]+#13;
showmessage(b);
s.free;
strstemp:tstringlist;
i:integer;
begin
strstemp:=tstringlist.Create ;
strstemp.Delimiter :=',';
strstemp.DelimitedText:=s;
for i:=0 to s.count-1 do
showmessage(s[i]);
strstemp.free
end;