String + String
G'day, I know that this is quite a basic question, but it has been stumping me, how can I add 2 strings together easily, or append a charecter to the end of a string?
Thanks,
John
[EDIT] To clarify, add 2 VARIABLES (String) together.
Thanks,
John
[EDIT] To clarify, add 2 VARIABLES (String) together.
Comments
Also code for appending to a string.
It is a bit more difficult to work with strings in Spin compared with some other languages so it is ok if you are finding it hard.
The propeller does not have dynamic memory allocation. This means when you want to add/append/concatenate strings you have to be sure that the buffer used to hold the result is big enough in advance!
@Dr_Acula:
Actually the basic equivalent of what you do inside the stringConcatenate-function is:
B$ = B$ + C$
return B$
The A$= part you mention is outside of the stringConcatenate and is optional.
stringConcatenate( @string1, @string2 ) would ommit the A$-part but still work and produce the same result
str_ptr := stringConcatenate( @string1, @string2 ) would do it A$=-style - but there is no point in doing this assignment.
With what I said in the first sentence something like this is a BUG:
str_ptr := stringConcatenate( string("Test"), string(" concatenation") ) because string("Test") does not reserve more bytes than needed for "Test" and the concatenation would overwrite bytes it should not touch. (using string() for the second parameter is fine!)
Yes, I agree you need to break down A$=B$+C$ into two parts A$=B$ and then A$ =A$+C$.
To add to the link to that string handling code (which is Kye's string handler plus a few Basic like instructions I added), in your "main" program you need to declare some generic string variables:
move a 'fixed' value into a string variable with
and then everything is referenced with the @ symbol. This was a whole lot of demo code I used to test strings. It is all commented at the moment but you can see some of the ideas how to manipulate strings
Kye's string object has an official name if you want to use the .spin object. See attachment.
Summary of the string instructions below. Most are Kye's and follow the format "instruction, destination, source" but some of the ones I wrote follow the Basic syntax of "source, destination".
Kye's "buildstring" has some additional tricks like being able to handle backspace.