Answer the question
In order to leave comments, you need to log in
How to split a string into an array by space?
Let's say there is a string:
Arbitrary string
It should be like this:
['Arbitrary', 'string']
Answer the question
In order to leave comments, you need to log in
You loop through the string, if there is a space, then from the beginning to the space you put it in an array.
I recommend that you first count the spaces in one cycle. Set array size = number of spaces + 1.
Next, fill the array.
You need a variable that will remember the last position
of LastPos
AND a variable of the last element
LastIndex
Well, here it is:
SetLength(a, SpaceCount + 1);
LastPos := 1;
LastIndex := 0;
for i := 1 to Length(Str) do
if Str[i] = ' ' then
begin
a[LastIndex] := Copy(Str, LastPos, (i - LastPos)); //-- тут возможно +-1
Inc(LastIndex);
LastPos := i;
end;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question