Playing with strings
jjames
Posts: 2,908
Let's admit it, without feedback we mind as well be programming a Harmony Remote. The user needs to see what's going on right? Well, thankfully there's a lot of InConcert files & modules to help us out with all this parsing. But when there isn't, it's up to us to figure it . . .and, I particually LOVE to parse strings. So here's some tricks I use.
Here's a three one-liner string manipulation codes. The first one I got from at P2.
(* TRIM x NUMBER OF CHARACTERS FROM END *)
Here's a three one-liner string manipulation codes. The first one I got from at P2.
(* TRIM x NUMBER OF CHARACTERS FROM END *)
cSTRING = SET_LENGTH_STRING(LENGTH_STRING(cSTRING)-x)(* REMOVE x NUMBER OF CHARACTERS FROM START *)
REMOVE_STRING(cSTRING,LEFT_STRING(cSTRING,x),1)(* REMOVE x NUMBER OF CHARACTERS IN THE MIDDLE OF STRING STARTING AT y*)
REMOVE_STRING(cSTRING,MID_STRING(cSTRING,y,x),y)
0
Comments
A better solution (in NetLinx) is GET_BUFFER_STRING.
Cool - much less typing. Didn't know about that one. Thanks!
Of course, these are the way I figured things, which might be a long way (as seen above.) If anyone has any suggestions, ideas - don't hesitate.
cSTRING = "ABCDEFGH"
x = 2
y = 4
MID_STRING(cSTRING,y,x) = "DE"
REMOVE_STRING(cSTRING,"DE",y) = "ABCDE"
cSTRING = "FGH"
No?
Someone on the board posted a FUNCTION to remove a word or phrase from a string. This can also be accomplished with a one-liner:
cSTRING = 'We are trying to remove this word from this phrase.'
REMOVE_STRING(cSTRING,'this word',FIND_STRING(cSTRING,'this word',1))
cSTRING now equals 'We are trying to remove from this phrase.'
Tech Note 659: TRIM_STRING
Tech Note 660: FIND_STRING_REV
Tech Note 661: REPLACE_STRING
Tech Note 662: SPLIT_STRING
SPLIT_STRING is a favorite of mine because it?s a simple way to parse data in a comma delimited file.
One example:
If you always need to have a string length of 2 (DOUBLE DIGITS) try this:
cVAR = RIGHT_STRING("'00', ITOA(nMIN)",2);
I use it to display time in two digits.
If ITOA(nMIN) is a single digit like 1-9, you'll get two digits 09, anything over a single digit will return as a standard two digit value because the '00' is first in the string. The same idea can be used for LEFT_STRING or MID_STRING!
09:05AM instead of 9:5AM.
-Gary
cVAR = FORMAT('%02d', nMIN)
Ahhh. Silly me, going by the NS online docs for Netlinx keywords... (<- tounge planted firmly in cheek)
- Chip