'获取字符串中的指定位置的子字符串,VB6 'S:字符串;SubStr:指定的分隔符(一个字符);ii:指定的位置;GetSubStr:获得的字字符串 'eg:GetSubStr("123,456,789,123",",",3)结果为字符串789 PublicFunction GetSubStr(S AsString, SubStr AsString, ii AsInteger) AsString Dim jj AsInteger, TempInt AsInteger IfInStr(1, S, SubStr, vbTextCompare) =0Then GetSubStr ="" ExitFunction EndIf TempInt =1 For jj =1To ii IfInStr(TempInt, S, SubStr, vbTextCompare) =0Then If jj < ii Then GetSubStr ="" Else GetSubStr =Mid(S, TempInt, Len(S) - TempInt +1) EndIf ExitFunction EndIf GetSubStr =Mid(S, TempInt, InStr(TempInt, S, SubStr, vbTextCompare) - TempInt) TempInt =InStr(TempInt, S, SubStr, vbTextCompare) +1 Next jj End Function
//获取字符串中的子字符串,Delphi7 //S:字符串;SubStr:指定的分隔符(一个字符);Index:指定的位置;GetSubStr:获得的字字符串 //eg:GetSubStr('123,456,789,123',',',3)结果为字符串789 function GetSubStr(S : string;SubStr : Char;Index : integer):string; var TempStr : string; I : integer; begin TempStr := S; if Pos(SubStr,S)=0 then Result :='' else begin try for I :=1 to Index -1do begin if Pos(SubStr,TempStr) =0 then begin Result :=''; Exit; end; Delete(TempStr,1,Pos(SubStr,TempStr)); TempStr := Trim(TempStr); end; except ShowMessage('Have not such Index!'); result :=''; Exit; end; if Pos(SubStr,TempStr)=0 then Result := TempStr else Result := Copy(TempStr,1,Pos(SubStr,TempStr) -1); end; end;