03-21-2012, 07:30 AM
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?
[EDIT] To clarify, add 2 VARIABLES (String) together.
03-21-2012, 07:48 AM
Seems to be a popular subject, have a look at this thread (http://forums.parallax.com/showthread.php?138326-string-string-combining-string) re: string combining.
03-21-2012, 09:22 AM
I had actually looked at that thread before, I'm quite a newbie to SPIN, and that didn't help much, I am wanting to add string VARIABLES together, not just plain string(..)'s.
03-21-2012, 09:32 AM
Post #12 on that thread above has the code to add variables. (ie the Basic equivalent of A$ = B$ + C$).
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.
03-21-2012, 10:31 AM
Maybe some more words about the stringhandling:
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!
Actually the basic equivalent of what you do inside the stringConcatenate-function is:
B$ = B$ + C$
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!)
03-21-2012, 11:31 AM
Good points MagIO2.
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:
byte LineOfText1 ' general purpose string buffer
byte LineOfText2 ' general purpose string buffer
byte LineOfText3 ' general purpose string buffer
move a 'fixed' value into a string variable with
str.copy(string("hello"),@lineoftext1) ' copy fixed value into a string
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
'printstring(@lineoftext1) ' testing string functions
'str.left(@lineoftext1,@lineoftext2,3) ' find leftmost characters in a string
'str.mid(@lineoftext1,@lineoftext2,4,2) ' find middle characters in a string
'str.copy(string("hello"),@lineoftext1) ' copy fixed value into a string
'str.str(@lineoftext1,55) ' decimal to string with no leading zeros
'str.str(@lineoftext1,1234)' convert to string
'str.str(@lineoftext1,str.decimaltointeger(@lineof text1)) ' convert back
' str.left(@lineoftext1,@lineoftext2,str.len(@lineof text1)-4) ' remove extension
' printstring(@lineoftext2) ' print it out
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.
PUB buildString(character) '' 4 Stack longs
PUB builtString(resetString) '' 4 Stack Longs
PUB builderNumber '' 3 Stack Longs
PUB builderFull '' 3 Stack Longs
PUB stringCompareCS(characters, otherCharacters) '' 5 Stack Longs
PUB stringCompareCI(characters, otherCharacters) '' 9 Stack Longs
PUB stringCopy(whereToPut, whereToGet) '' 5 Stack Longs
PUB stringConcatenate(whereToPut, whereToGet) '' 5 Stack Longs
PUB stringToLowerCase(characters) '' 4 Stack Longs
PUB stringToUpperCase(characters) '' 4 Stack Longs
PUB trimString(characters) '' 8 Stack Longs
PUB tokenizeString(characters) '' 8 Stack Longs
PUB findCharacter(stringToSearch, characterToFind) '' 5 Stack Longs
PUB replaceCharacter(stringToSearch, characterToReplace, characterToReplaceWith) '' 11 Stack Longs
PUB replaceAllCharacters(stringToSearch, characterToReplace, characterToReplaceWith) '' 17 Stack Longs
PUB findString(stringToSearch, stringToFind) | index, size '' 7 Stack Longs
PUB replaceString(stringToSearch, stringToReplace, stringToReplaceWith) '' 13 Stack Longs
PUB replaceAllStrings(stringToSearch, stringToReplace, stringToReplaceWith) '' 19 Stack Longs
PUB integerToDecimal(number, length) '' 5 Stack Longs
PUB integerToHexadecimal(number, length) '' 5 Stack Longs
PUB integerToBinary(number, length) '' 5 Stack Longs
PUB decimalToInteger(characters) | sign '' 10 Stack Longs
PUB hexadecimalToInteger(characters) | sign '' 10 Stack Longs
PUB binaryToInteger(characters) | sign '' 10 Stack Longs
PRI ignoreCase(character) ' 4 Stack Longs
PRI ignoreSpace(characters) ' 4 Stack Longs
PRI checkSign(characters, signAddress) ' 5 Stack Longs
PRI checkDigit(characters, low, high) ' 5 Stack Longs
PUB endsWithString(stringToSearch, stringToFind) '' 12 Stack Longs
PUB Left(Source, Destination,Number) ' returns the left number of characters
PUB Len(Source) ' returns the length of the string
PUB Mid(Source,Destination,Start,Number) ' returns strings starting at start with number characters
PUB Copy(Source,Destination) ' reverse order to stringcopy. Can also use to create strings eg copy(string("test"),@lineoftext1)
PUB Str(Destination,Number) | n' convert a number to a string representation. Uses Basic syntax, ie leading character is a space if +ve, and is - if negative
PUB Stringfill(Destination, Number,AsciiValue)' fill string with a string, eg 3,65 is"AAA" and 2,32 is " "
PUB Instr(Source,AsciiValue) | j ' find asciivalue in Source. Returns 0 if not found