谢谢 轻舞肥羊, ExtractStrings让我见识到Borland公司的程序员写代码是多么厉害, 对比我和他的翻译, 真是天壤之别.谢谢 关门放狗, 我要的就是这种风格的翻译.但是您翻译的这个函数试验了一下是死循环.谢谢 wangdonghai 您的翻译也是另一种不错的思路.整理如下:// Borland公司的程序员:function ExtractStrings(Separators, WhiteSpace: TSysCharSet;
Content: PChar;
Strings: TStrings): Integer;var Head, Tail: PChar;
EOS, InQuote: Boolean;
QuoteChar: Char;
Item: string;
begin
Result := 0;
if (Content = nil) or (Content^=#0) or (Strings = nil) then
Exit;
Tail := Content;
InQuote := False;
QuoteChar := #0;
Strings.begin
Update;
try repeat while Tail^ in WhiteSpace + [#13, #10]do
Tail := StrNextChar(Tail);
Head := Tail;
while Truedo
begin
while (InQuote and not (Tail^ in [QuoteChar, #0])) or not (Tail^ in Separators + [#0, #13, #10, '''', '"'])do
Tail := StrNextChar(Tail);
if Tail^ in ['''', '"'] then
begin
if (QuoteChar <> #0) and (QuoteChar = Tail^) then
QuoteChar := #0 else
if QuoteChar = #0 then
QuoteChar := Tail^;
InQuote := QuoteChar <> #0;
Tail := StrNextChar(Tail);
end else
Break;
end;
EOS := Tail^ = #0;
if (Head <> Tail) and (Head^ <> #0) then
begin
if Strings <> nil then
begin
SetString(Item, Head, Tail - Head);
Strings.Add(Item);
end;
Inc(Result);
end;
Tail := StrNextChar(Tail);
until EOS;
finally Strings.EndUpdate;
end;
end;
procedure TForm1.Button12Click(Sender: TObject);var s : string;
gg : array of string;
lst : TStrings;
i : Integer;
begin
s := '爷爷,,奶奶,,,,,,,爸爸,,,妈妈,,,me';
lst := TStringList.Create;
try ExtractStrings([','], [], PChar(s), lst);
SetLength(gg, lst.Count);
for i := 0 to lst.Count - 1do
gg
:= lst;
finally lst.Free;
end;
for i := 0 to High(gg)do
memo1.Lines.Add(gg);
end;
function getmystr(s: PChar;
dl: PChar;
arrText: array of pchar;
len: Integer): Integer;var n: Word;
begin
arrText[0] := s;
while (Integer(s^) <> 0)do
begin
// if ((strchr(dl, (int) *s) != NULL) && n < len) { if (Pos(string(s^), StrPas(dl)) > 0) and (n < len) then
begin
s^ := #0;
while ((s + 1)^ <> '0') and (Pos(string((s + 1)^), StrPas(dl)) > 0)do
Inc(s);
inc;
arrText[n] := s + 1;
end;
if (s^ = #13) and (s^ = #10) then
begin
s^ := #0;
exit;
end;
end;
end;
procedure TForm1.Button13Click(Sender: TObject);var n, i, j: Integer;
dl: Array [0..15] of pchar;// [0..19]begin
n := getmystr('爷爷,,奶奶,,,,,,,爸爸,,,妈妈,,,me', ',', dl, 20);
Memo1.Lines.Add(format('n = %d %s %s %s %s ', [n, dl[0], dl[1], dl[2], dl[3]]));
end;
//下面的代码只处理分隔符为一个字符的情况procedure GetMyStr2(s: PChar;
dl: char);var c: array[0..255] of char;
p: PChar;
i1, i2: Integer;
begin
p := s;
i1 := 0;
i2 := 0;
while p^ <> #0do
begin
if p^ = dl then
begin
if i2 - i1 > 1 then
begin
StrLCopy(c, s + i1, i2 - i1);
showmessage(c);
end;
i1 := i2 + 1;
end;
inc(p);
inc(i2);
end;
if i1 < i2 then
//末尾没有分隔符 begin
StrLCopy(c, s + i1, i2 - i1);
showmessage(c);
end;
end;
procedure TForm1.Button14Click(Sender: TObject);
begin
GetMyStr2(PChar('爷爷,,奶奶,,,,,,,爸爸,,,妈妈,,,me'),',');
end;
我还想看看各位处理字符串的高手还要没有其他更理想的翻译, 再等一两天再结贴吧.谢谢您们!