Page 1 of 1

Chopping up a string in separate pieces

Posted: 03 May 2012
by JoshM
The function below "chops up" (cut into pieces; "chop wood"; "chop meat") a string by replacing each space (" ") with an Enter (i.e. NewLine):

Code: Select all

{
StrChopUp
---------
This function "chops up" a string in separate pieces, where each space (" ") is replaced by a new line.

Changelog
---------
3-5-2012 First version.
}

Inputs:
StrToChopUp(StringSimple);

Variables:
x(0), rawStr("");

rawStr = StrToChopUp;
x = InStr(rawStr, " ");

while (x <> 0) begin

x = InStr(rawStr, " ");

if (x > 0) then
rawStr = Text(LeftStr(rawStr, x - 1), NewLine, MidStr(rawStr, x + 1, StrLen(rawStr) - x));

end;

StrChopUp = rawStr;
Function Storage: Simple, Return Type: String.

For example:

Code: Select all

if (LastBarOnChart_s = True) then begin

Print(StrChopUp("The 'StrChopUp' function 'chops up' a string in a line for each word, which may sometimes come in handy."));

end;
returns:

Code: Select all

The
'StrChopUp'
function
'chops
up'
a
string
in
a
line
for
each
word,
which
may
sometimes
come
in
handy.
Not earth-shattering, but perhaps sometimes handy. :)