Author Topic: How to add onto a existing string?  (Read 1023 times)

For example
We have
%variable="elephant";
But we want to add onto the variable like so:
%variable="blue elephant"
Or
%variable="China";
and
%variable="China Lake";
And in between also
%variable="big elephant";
and
%variable="big blue elephant";

Is there a better way to do this other than
%variable="blue" SPC %variable;

Something called addtostring lingers in my memory, but i'm not sure from where I remember it from and if it's real, then how to use it.

Adding onto a string is called concatenation. There's two concatenation operators in Torque: @ and " SPC ". @ will link two strings together directly, and " SPC " will link things together with a space in between.

For example:

%var = "blue" @ " " @ "elephant";

or:

%var = "blue" SPC "elephant";.

The reason I'm referring to the concatenation with space operator as " SPC " is because it requires a space before and after "SPC". So, while all other operators don't require a space (e.g. 5+2, "hi"@"you") that one does.

There is a third concatenation operator, NL, which starts a new line.

There is a fourth concatenation operator, TAB, which inserts a horizontal tab.

Yes, of the @, SPC and NL i'm aware of. I was wondering if there was a hard feature in Torque to add onto a string, specifically to the middle of one.
NOT ACTUAL CODE Something like %variableaddtostring("elephant") to make Blue to Blue elephant. Maybe someone can approach this from the GetWord aspect or..

Edit:
There is a fourth concatenation operator, TAB, which inserts a horizontal tab.
Huh, i did not know that.. Neat

==>%text = "this is a test";
==>%text = setWord(%text, 1, "was");
==>echo(%text);
this was a test

So , 0, for the beginning of the string, 1 for after the first word and so on? Does it automatically correctly enter spaces also?

It replaces a word. Use it in combination with getWord to insert a word.

Oh, my bad, missed that. That makes more sense. I'll continue tinkering with my add-on. Thanks for the help everyone.

Wowza can't believe I forgot about NL and TAB. I feel dumb.

Anyway, concatenation operators ARE Torque's "hard feature" for adding onto strings. Technically, a function to do so would be considered a "soft" feature in comparison. "Something like %variableaddtostring("elephant") to make Blue to Blue elephant" would be %varaible = %variable SPC "elephant";
« Last Edit: November 09, 2014, 03:21:37 PM by $trinick »