Home AMX User Forum AMXForums Archive Threads Tips and Tricks

Playing with strings

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 *)
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)

Comments

  • jjames wrote:
    (* REMOVE x NUMBER OF CHARACTERS FROM START *)
    REMOVE_STRING(cSTRING,LEFT_STRING(cSTRING,x),1)
    

    A better solution (in NetLinx) is GET_BUFFER_STRING.
    GET_BUFFER_STRING(cSTRING,x)
    
  • jjamesjjames Posts: 2,908
    Nice one!

    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.
  • jjames wrote:
    (* REMOVE x NUMBER OF CHARACTERS IN THE MIDDLE OF STRING STARTING AT y*)
    REMOVE_STRING(cSTRING,MID_STRING(cSTRING,y,x),y)
    

    cSTRING = "ABCDEFGH"
    x = 2
    y = 4

    MID_STRING(cSTRING,y,x) = "DE"

    REMOVE_STRING(cSTRING,"DE",y) = "ABCDE"
    cSTRING = "FGH"

    No?
  • jjamesjjames Posts: 2,908
    No, cSTRING would equal 'ABCFGH'. See Tech Note #538; your example is the "old" way of things.

    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.'
  • Joe HebertJoe Hebert Posts: 2,159
    Keeping in the theme of playing with strings, there are a few built in functions that exist in other languages that don?t natively exist in Netlinx. AMX posted code for these functions in Tech Notes which I normally include in a Globals.axi. They are:

    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.
  • GSLogicGSLogic Posts: 562
    This one is VERY useful!

    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
  • Joe HebertJoe Hebert Posts: 2,159
    GSLogic wrote:
    This one is VERY useful!

    One example:
    If you always need to have a string length of 2 (DOUBLE DIGITS) try this:

    cVAR = RIGHT_STRING("'00', ITOA(nMIN)",2);
    There?s always more than one way to skin a cat. Here's one alternative that will yield the same results:
    cVAR = FORMAT('%02d', nMIN)
  • jjames wrote:
    No, cSTRING would equal 'ABCFGH'. See Tech Note #538; your example is the "old" way of things.

    Ahhh. Silly me, going by the NS online docs for Netlinx keywords... :) (<- tounge planted firmly in cheek)

    - Chip
Sign In or Register to comment.