Questa funzione è progettata per non unire separatori consecutivi. Per esempio, pensare di dividere la seguente stringa su virgole:
foo,,bar
Che cosa vi aspettate SplitString('foo,,bar', ',')
di tornare? Vorresti cercare ('foo', 'bar')
o la risposta sarebbe ('foo', '', 'bar')
? Non è chiaro a priori che sia giusto, e casi d'uso diversi potrebbero volere output diversi.
Se il tuo caso, hai specificato due delimitatori, ';'
e ' '
. Ciò significa che
'a'; 'b'
divide in ';'
e di nuovo a ' '
. Tra questi due delimitatori non c'è nulla e quindi viene restituita una stringa vuota tra 'a'
e 'b'
.
Il metodo Split
dal string helper introdotto in XE3 ha un parametro TStringSplitOptions
. Se si passa ExcludeEmpty
per quel parametro, i separatori consecutivi vengono considerati come un singolo separatore. Questo programma:
{$APPTYPE CONSOLE}
uses
System.SysUtils;
var
S: string;
begin
for S in '''a''; ''b''; ''c'''.Split([';', ' '], ExcludeEmpty) do begin
Writeln(S);
end;
end.
uscite:
'a'
'b'
'c'
Ma voi non avete questo a vostra disposizione in XE2 quindi penso che si sta andando ad avere per rotolare il proprio funzione split. Quale potrebbe essere il seguente:
function IsSeparator(const C: Char; const Separators: string): Boolean;
var
sep: Char;
begin
for sep in Separators do begin
if sep=C then begin
Result := True;
exit;
end;
end;
Result := False;
end;
function Split(const Str, Separators: string): TArray<string>;
var
CharIndex, ItemIndex: Integer;
len: Integer;
SeparatorCount: Integer;
Start: Integer;
begin
len := Length(Str);
if len=0 then begin
Result := nil;
exit;
end;
SeparatorCount := 0;
for CharIndex := 1 to len do begin
if IsSeparator(Str[CharIndex], Separators) then begin
inc(SeparatorCount);
end;
end;
SetLength(Result, SeparatorCount+1); // potentially an over-allocation
ItemIndex := 0;
Start := 1;
CharIndex := 1;
for CharIndex := 1 to len do begin
if IsSeparator(Str[CharIndex], Separators) then begin
if CharIndex>Start then begin
Result[ItemIndex] := Copy(Str, Start, CharIndex-Start);
inc(ItemIndex);
end;
Start := CharIndex+1;
end;
end;
if len>Start then begin
Result[ItemIndex] := Copy(Str, Start, len-Start+1);
inc(ItemIndex);
end;
SetLength(Result, ItemIndex);
end;
Naturalmente, tutto ciò presuppone che si desideri che uno spazio operi come separatore. L'hai chiesto nel codice, ma forse in realtà vuoi solo che ;
funga da separatore. In tal caso, probabilmente si desidera passare ';'
come separatore e tagliare le stringhe restituite.
Leggi i documenti. Forse non come previsto, ma funziona come documentato. –
Questa domanda ha alcuni suggerimenti sulla divisione di una stringa, basata su una stringa multi-carattere (cosa che si aspettava che stesse facendo), ma la maggior parte di essi funziona con liste di stringhe, non con array: http://stackoverflow.com/questions/15424293/how-to-split-string-by-a-multi-character-delimiter – quasoft