procedure TForm1.Button1Click(Sender: TObject); var str:string; ss:array[0..5] of String; begin str:='111/222/333/444/555.wav'; ReadStrValues(str,ss,'/'); showmessage(Format('[%s],[%s],[%s],[%s],[%s]',[ss[0],ss[1],ss[2],ss[3],ss[4]])); end;procedure TForm1.ReadStrValues(Str: string; var StrArr: array of String; Ch: string = #9); var //去制表符,参数为string指针 例如 @s1,@s2 可传多个参数,因为是数组 //ReadStrValues(str,[@s1,@s2,@s3]);多少个参数都可以 I,b, StrLen, StrPos, ArrLen, ArrIndex: Integer; begin if str = '' then exit; StrLen := Length(Str); // 字符串长 ArrLen := Length(StrArr); // 待取出的字符串个数 if ArrLen = 0 then Exit; // 如果字符串指针数组中无元素,则直接退出 StrPos := 1; // 当前读到的字符串位置 ArrIndex := 0; // 当前字符串指针数组位置 b:=0; // 用于记录连续在一起的制表符。 // 此处将 Str 长度加1,用于后面判断是否已读完整个字串 for I := 1 to StrLen + 1 do begin // 注意此处条件位置不可倒置(骤死式判断) if (I = StrLen + 1) or (Str[I] = Ch) or (Str[I] = ' ') or (str[i]=',') then begin // 拷贝读到的字符串 if (str[i] = ch) and(str[i+1] = ch) then b:=b+1 //遇到连续的制表符,记录下来 else begin StrArr[ArrIndex] := Copy(Str, StrPos, I - StrPos-b); //-b把连续的制表符减掉 StrPos := I + 1; b:=0; //如果没发现连续的制表符,清掉记录 // 如果需要读的字符串指针数组已完成,则退出 Inc(ArrIndex); end; if ArrIndex >= ArrLen then Exit; end; end; // 检查是否所有的字符串指针都读到了,如果没有读到,将被设置为空串 for I := ArrIndex to ArrLen - 1 do StrArr[I] := ''; end;
procedure TForm1.Button1Click(Sender: TObject);
var
str:string;
ss:array[0..5] of String;
begin
str:='111/222/333/444/555.wav';
ReadStrValues(str,ss,'/');
showmessage(Format('[%s],[%s],[%s],[%s],[%s]',[ss[0],ss[1],ss[2],ss[3],ss[4]]));
end;procedure TForm1.ReadStrValues(Str: string; var StrArr: array of String; Ch:
string = #9);
var //去制表符,参数为string指针 例如 @s1,@s2 可传多个参数,因为是数组
//ReadStrValues(str,[@s1,@s2,@s3]);多少个参数都可以
I,b, StrLen, StrPos, ArrLen, ArrIndex: Integer;
begin
if str = '' then exit;
StrLen := Length(Str); // 字符串长
ArrLen := Length(StrArr); // 待取出的字符串个数
if ArrLen = 0 then Exit; // 如果字符串指针数组中无元素,则直接退出 StrPos := 1; // 当前读到的字符串位置
ArrIndex := 0; // 当前字符串指针数组位置
b:=0; // 用于记录连续在一起的制表符。
// 此处将 Str 长度加1,用于后面判断是否已读完整个字串
for I := 1 to StrLen + 1 do
begin
// 注意此处条件位置不可倒置(骤死式判断)
if (I = StrLen + 1) or (Str[I] = Ch) or (Str[I] = ' ') or (str[i]=',') then
begin
// 拷贝读到的字符串
if (str[i] = ch) and(str[i+1] = ch) then
b:=b+1 //遇到连续的制表符,记录下来
else
begin
StrArr[ArrIndex] := Copy(Str, StrPos, I - StrPos-b); //-b把连续的制表符减掉
StrPos := I + 1;
b:=0; //如果没发现连续的制表符,清掉记录
// 如果需要读的字符串指针数组已完成,则退出
Inc(ArrIndex);
end;
if ArrIndex >= ArrLen then Exit;
end;
end; // 检查是否所有的字符串指针都读到了,如果没有读到,将被设置为空串
for I := ArrIndex to ArrLen - 1 do
StrArr[I] := '';
end;