Split string after first character
ryfitzger227
Posts: 99
Hello!
I've did some searching on this and found one other thread on here, but it doesn't explain how to do what I'm wanting to do.
I'm sending a string via serial port to the Propeller of "1ABCDEFG". I'm wanting to be able to split the string into two variables. The first character will be the "type variable" and anything after that will be the "content variable."
So this is what I want:
MainVar = 1ABCDEFG (sent from serial port)
TypeVar = 1 (first character in string MainVar)
ContentVar = ABCDEFG (everything after the first character in MainVar)
Any idea on how to do this?
I've did some searching on this and found one other thread on here, but it doesn't explain how to do what I'm wanting to do.
I'm sending a string via serial port to the Propeller of "1ABCDEFG". I'm wanting to be able to split the string into two variables. The first character will be the "type variable" and anything after that will be the "content variable."
So this is what I want:
MainVar = 1ABCDEFG (sent from serial port)
TypeVar = 1 (first character in string MainVar)
ContentVar = ABCDEFG (everything after the first character in MainVar)
Any idea on how to do this?
Comments
Andy
One thing I'm doing is sending a string from the serial port. It's not always going to be 1ABCDEFG but it could also be 2GFEDCBA, 112345678, and so on. I also don't want to just display it in the serial port, but rather do something with it - if TypeVar == 1....
Here's what I used.
The only problem with this is that when I receive the second line (serial.str(StringAddress+2)) on the computer it comes up as .
Also, when I change StringAddress to readBuffer it doesn't work, and that's really what I need (since the Serial Port is setting the string and it's not already set). Another thing that didn't work was the code in red. I want to see if the first character equals one. If so, light an led.
Anyone got anything that could fix these problems? If you have any questions, please ask.
Last thing and this should finalize this part of the project. I'm trying to use the ContentVar as an integer value to set a pause in the code. The way it is supposed to work is the string sent from the computer will be somewhere along the lines of "11000". The first character being the TypeVar ("1") and the rest ("1000") being the time, in ms, to pause for. How do I transfer ContentVar to an integer that waintcnt can use? - I think I'm saying that right. Haha.
Thanks.
- Ryan
EDIT: Sometimes the Type can also be a letter, so think of the string sent from the computer as "A1000" A being the TypeVar and 1000 being the ContentVar, or time in ms to pause for.
I just did a search on the forum search tool for str2dec and this was one of many things that came up that may get you started. Get the string converted to a decimal, then convert the decimal to the wait period you need.
More ideas:
Thanks so much for that code. I din't really know what to search for in this, so this helped a lot. The second code block worked perfect.
Thanks again,
- Ryan