获取字符串中的指定位置的子字符串,在MIF/MID文件中接触这样的字符串比较多,所以写成了一个函数,比较EASY。在VB6和Delphi7中测试通过。
'获取字符串中的指定位置的子字符串,VB6
'S:字符串;SubStr:指定的分隔符(一个字符);ii:指定的位置;GetSubStr:获得的字字符串
'eg:GetSubStr("123,456,789,123",",",3)结果为字符串789
Public Function GetSubStr(S As String, SubStr As String, ii As Integer) As String
Dim jj As Integer, TempInt As Integer
If InStr(1, S, SubStr, vbTextCompare) = 0 Then
GetSubStr = ""
Exit Function
End If
TempInt = 1
For jj = 1 To ii
If InStr(TempInt, S, SubStr, vbTextCompare) = 0 Then
If jj < ii Then
GetSubStr = ""
Else
GetSubStr = Mid(S, TempInt, Len(S) - TempInt + 1)
End If
Exit Function
End If
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 - 1 do 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;