Аналог string s.Split(..) в Delphi

ppkp

Member
Joined
May 11, 2006
Messages
26
Reaction score
4
Понимаю, что это парсер, но именно такой, а не навороченный я ищу.
Подскажите, где взять исходники сего чуда.
 

ppkp

Member
Joined
May 11, 2006
Messages
26
Reaction score
4
Разбивает строку на подстроки в соответствии с заданными разделителями
 

brs

Member
Joined
Oct 12, 2006
Messages
6
Reaction score
0
list:=TStringList.Create;
list.Delimiter:=';';
list.Text:='1;2;3';

for I := 0 to List.Count - 1 do
result:=list.Strings;
 

ppkp

Member
Joined
May 11, 2006
Messages
26
Reaction score
4
Спасибо, Совсем забыл про эти properties. Только подправил

List.Delimiter := ',';
List.QuoteChar := '"';
List.DelimitedText := '"asa. sds bb","aaa «Delphi» cc",4.6,5.00,3';

Но это работает только с одним типом Delimiter, а универсальности нет.
Спасибо большое.
 

brs

Member
Joined
Oct 12, 2006
Messages
6
Reaction score
0
Вот еще

function StringSplit(Str,Delim:String):TStrings;
var
I,Start,Count: Integer;
begin
Result := TStringList.Create;
Start := 1;
I := 1;
Count := 0;
While I < Length(Str) do
begin
if Copy(Str,I,Length(Delim)) = Delim then
begin
Result.Add(Copy(Str,Start,I - Start));
I := I + Length(Delim);
Start := I;
end
else
I := I + 1;
end;
if Length(Str) > 0 then
Result.Add(Copy(Str,Start,Length(Str)));
end;
 

ppkp

Member
Joined
May 11, 2006
Messages
26
Reaction score
4
Спасибо, большое спасибо. В принципе вариант с TStrings.DelimitedText подходит для всех нужных типов разделителей. А с TStringGrid Вы работали ? Мне необходимо перетащить Selected в другое место этой же таблицы (a-la Excel).
 

dxsoft

Member
Joined
Apr 23, 2010
Messages
11
Reaction score
88
Here some Code I usually Use
I Hope this will be help you
type
TStringArray = array of String;

function BuffTextEqualAsm(const Str1, Str2: PChar; MaxLen: Cardinal): Boolean; assembler;
asm
PUSH EDI
PUSH ESI
PUSH EBX
MOV EDI,EDX
MOV ESI,EAX
MOV EBX,ECX
XOR EAX,EAX
OR ECX,ECX
JE @@T
REPNE SCASB
SUB EBX,ECX
MOV ECX,EBX
MOV EDI,EDX
XOR EDX,EDX
@@1:
REPE CMPSB
JE @@T
MOV AL,[ESI-1]
CMP AL,'a'
JB @@2
CMP AL,'z'
JA @@2
SUB AL,20H
@@2:
MOV DL,[EDI-1]
CMP DL,'a'
JB @@3
CMP DL,'z'
JA @@3
SUB DL,20H
@@3:
SUB EAX,EDX
JE @@1
@@F:
xor EAX,EAX
jmp @@E
@@T:
mov EAX,1
@@E:
POP EBX
POP ESI
POP EDI
end;

function IndexedWithToken(const Src: String;const Index: Integer;Token:String = ','): String;
var
P, Start : PChar;
tkLength : Integer;
LocalIndex : Integer;
begin
Result := '';
LocalIndex := 1;
tkLength := Length(Token);
P := Pointer(Src);
Start := P;
if Index <= 0 then Exit;

if P <> nil then begin
while P^ <> #0 do
begin
while Not ((P^ In [#0]) or BuffTextEqualAsm(P,Pointer(Token),tkLength)) do Inc(P);
if P <> Start then
begin
if LocalIndex = Index then
begin
SetString(Result, Start, Integer(P - Start));
Exit;
end;
end;
if P^ = #0 then break;
Inc(LocalIndex);
Inc(P,tkLength);
Start := P;
end;
end;
if P <> Start then
begin
if LocalIndex = Index then
begin
SetString(Result, Start, Integer(P - Start));
Exit;
end;
end;
end;

procedure StringSplit(SourceStr,SplitStr: string;var SplitStrs: TStringArray);
var
i: Integer;
str: string;
begin
i := 1;
while True do
begin
str := IndexedWithToken(SourceStr,i,SplitStr);
if str <> '' then
begin
SetLength(SplitStrs,i);
SplitStrs[i-1] := str;
end
else Break;
Inc(i);
end;
end;
 

Alexonix

Member
Joined
Nov 18, 2010
Messages
7
Reaction score
0
list.Delimiter:=';'; - к сожалению еще пробелы всегда за разделитель считает и новую строку.
 

cg_shura

Member
Joined
Oct 16, 2008
Messages
9
Reaction score
0
{ Заполняет список List словами, выделенных из строки S,
разделителями слов являются любое кол-во символов из множества Delimiters.

Возвращает List. Чтобы можно было писать примерно следующим образом

sl := FillWordsList(SomeStr, SomeDelim, TStringList.Create);
try
Do something
finally
sl.Free;
end;
}
function FillWordsList(const S: string; const Delimiters: TSysCharSet; List: TStrings): TStrings;
var
I, J, Len: Integer;
Word: string;
begin
Assert(List <> nil);

Len := Length(S);
I := 1;

List.BeginUpdate;
try

while I <= Len do
begin
{прогнать token}
J := I;

while (I <= Len) and not (S in Delimiters) do
Inc(I);

{ if I = J then
Break;}

Word := Trim(Copy(S, J, I - J));

if Word <> '' then
List.Add(Word);

{прогнать delimiters}
while (I <= Len) and (S in Delimiters) do
Inc(I);
end;

finally
List.EndUpdate;
end;

Result := List;
end;
 

ADnh47adhn

Banned
BANNED
Joined
Mar 30, 2009
Messages
10
Reaction score
6
к сожалению еще пробелы всегда за разделитель считает и новую строку
следует выставить TStrings.StrictDelimiter в False (идиотская фича новых RTL)
а вообще я бы посоветовал функцию SysUtils.ExtractStrings как максимально гибкий способ это сделать
 

NevermoreBY

Member
Joined
Nov 21, 2011
Messages
7
Reaction score
0
Тот пример asm, не хочет работать, у кого-нить были с ним проблемы?
 

Gaugan

New member
Joined
Aug 14, 2007
Messages
4
Reaction score
0
"Hey, for Delphi, you can use the 'Split' function from the System.SysUtils unit, it's pretty similar to string s.Split(..). Or you can also use regular expressions from the RegExpr unit. Both should do the trick."
 
Top