Chopping up a string in separate pieces

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Chopping up a string in separate pieces

Postby JoshM » 03 May 2012

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. :)

Return to “User Contributed Studies and Indicator Library”