String operations? substr, strreplace, etc?
dujoducom
Posts: 11
I come from a software background so I'm used to plenty of great string functions, why are they not available on the propeller? Am I just not looking in the right spot? I'm trying to parse a number out of some serial data to convert it to a value for doing some calculations. The·string is formatted something like this:
+0 012345000000
I'm using the + as a delimiter and getting the whole message using a serial buffer (well, using FullDuplexSerialExtnededs str method). Now I'm having issues parsing out only the 5 values·between the 0's (the 12345). In a higher level environment I would just use substring, starting at index 4 with a lenght of 5, but I can't seem to find anything in the manual other than strcomp and·strsize. I searched the forum and object exchange to no avail. Is it something so incredibly obvious that it's glossed over somewhere, or will it be more complicated than I thought?
Post Edited (dujoducom) : 9/25/2008 7:09:43 PM GMT
+0 012345000000
I'm using the + as a delimiter and getting the whole message using a serial buffer (well, using FullDuplexSerialExtnededs str method). Now I'm having issues parsing out only the 5 values·between the 0's (the 12345). In a higher level environment I would just use substring, starting at index 4 with a lenght of 5, but I can't seem to find anything in the manual other than strcomp and·strsize. I searched the forum and object exchange to no avail. Is it something so incredibly obvious that it's glossed over somewhere, or will it be more complicated than I thought?
Post Edited (dujoducom) : 9/25/2008 7:09:43 PM GMT
Comments
The main reason is that things like substr are indeed complex and require some kind of memory manager to provide storage for the result. It's too complex and would need to make too many assumptions to be included in Spin. Someone with the need or desire could certainly write an object that provides a string library that would be used much like the existing floating point library.
Thanks once again, will report back with any useful info I stumble across for future newbies [noparse]:)[/noparse]
-Phil
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
'Still some PropSTICK Kit bare PCBs left!
I, too, am used to using strings a lot and need to manipulate them as needed (habits taken from PHP and other places).
I'm sure I have some more laying around somewhere. I will post them here if I run across them.
PHP's function strstr should be easy to do as well, str_replace is more work, but should be possible as well as many other string function. A string library would be nice!
Post Edited (Bobb Fwed) : 9/26/2008 3:25:43 PM GMT
example (for those not familiar with the PHP function):
str := "test@example.com"
search := "@"
strstr(@str, @search, 0) ---> returns "@example.com"
returns false if search string not found
and offset can be used to "ignore" a certain number of characters off the beginning of str
returns the byte number in the string that matches search
returns -1 if search is not found
Phil- you should definitely post your strings object on the object exchange, it's packed full of string modifying goodness!