Home AMX User Forum NetLinx Studio

strings

I have a variable that holds a telephone number, including the hyphens (-). The problem is that the device that I'm goin to sen the string, needs the numbers one next to each other, without the hyphens.
If there is someone that knows how to go arround this, I'll be glad to listen.
I tried this, num_mem3=remove_string (TempDial, '-', 1), but is not working.

Please advice...

Thanks

Comments

  • mpullinmpullin Posts: 949
    I have a variable that holds a telephone number, including the hyphens (-). The problem is that the device that I'm goin to sen the string, needs the numbers one next to each other, without the hyphens.
    If there is someone that knows how to go arround this, I'll be glad to listen.
    I tried this, num_mem3=remove_string (TempDial, '-', 1), but is not working.
    REMOVE_STRING(haystack, needle, start) only removes the first instance of a needle from haystack, returns needle and everything before needle, and leaves everything in haystack after the needle, in haystack. You'll want to call it several times. First declare some variables to hold the chucks of the phone number.
    STACK_VAR CHAR firstpart[4];
    STACK_VAR CHAR secondpart[4];
    STACK_VAR CHAR thirdpart[4];
    
    Okay let's say we have a variable 'haystack' which is a string containing '864-555-1234'. First we split up the parts using REMOVE_STRING().
    firstpart = REMOVE_STRING(haystack,'-',1); // firstpart = '864-'
    secondpart = REMOVE_STRING(haystack,'-',1); // secondpart = '555-'
    thirdpart = haystack; // thirdpart = '1234'
    
    How do we get rid of the needles? Since we are trying to isolate numbers, a common trick is to convert into an integer and then convert back. Converting to an integer ignores non-number characters. If these were not numbers, you would use LEFT_STRING to get rid of the needles.
    firstpart = itoa(atoi("firstpart"));
    secondpart = itoa(atoi("secondpart"));
    
    Put them all together.
    SEND_STRING dvDEVICE, "firstpart,secondpart,thirdpart"
    
  • jjamesjjames Posts: 2,908
    String manipulation is my favorite topic. You can do this one of two ways easily; the first is a round about way of doing it, but using only NetLinx functions:
    REMOVE_STRING(TempDial,'-',FIND_STRING(TempDial,'-',1))
    

    The way Remove_String works is that it removes UP TO AND INCLUDING what you're looking for starting from a specific character index (the third parameter.) So, by replacing the third parameter with a FIND_STRING, which will return where what you're looking for is, it'll remove the string/char that we want to remove (the second parameter).

    Now, here's another way of doing it with a function that's been floating around for quite some time. Just as simple to use also.
    (***********************************************************)
    (* FUNCTION:     STRING_REMOVE                             *)
    (* RETURN:       STRING                                    *)
    (* PARAMETERS:   strSTR - SOURCE STRING                    *)
    (*               strSEARCH - SEARCH STRING                 *)
    (***********************************************************)  
    DEFINE_FUNCTION CHAR[2000] STRING_REMOVE(CHAR strSTR[], CHAR strSEARCH[])
    STACK_VAR
        CHAR strTEMP[2000]
    {
        strTEMP = STRING_REPLACE(strSTR, strSEARCH, '')
        RETURN strTEMP
    }
    

    As you see, it's your choice on how you wish to skin the cat. ;)
  • Remove_string removes everything up to and including the arguement character.
    num_mem3=remove_string (TempDial, '-', 1)
    

    With this, num_mem3 will equal everything up to and including the dash. There are lots of ways to get rid of the dash, heres one:

    So lets say this is your string '612-445-1234'
    DEFINE_FUNCTION CHAR[] fnDashRemove(CHAR sArgString)
    {
    STACK_VAR INTEGER num_part1
    STACK_VAR INTEGER num_part2
    STACK_VAR INTEGER num_part3
    STACK_VAR CHAR phone_num[10]
    
    num_part1 = ATOI(remove_string(sArgString,'-',1))
    num_part2 = ATOI(remove_string(sArgString,'-',1))
    num_part3 = ATOI(sArgString)
    
    phone_num = "ITOA(num_part1),ITOA(num_part2),ITOA(num_part3)"
    
    RETURN phone_num;
    }
    
    

    The ATOI function will convert only ASCII representations of numbers to an integer value, any non-number character (in this case the dash) will be dropped.

    This function will return '6124451234'

    If you are going to be sending it strings with more then two dashes this function will not work, but it should give you a general idea.
  • GSLogicGSLogic Posts: 562
    still skinning the cat!
    local char phoneNum[20];
    stack_var integer i char newString[20];
    
    phoneNum = "'555-7e89-89!8*/*8'";
    
    for(i=1; i<=length_string(phoneNum); i++)
    {
        if("phoneNum[i]" >= '0' and "phoneNum[i]" <= '9')
            newString = "newString, phoneNum[i]";
    }
    send_string 0, "'newString: ', newString, ' - phoneNum: ', phoneNum ";
    
    this will eliminate any characters that are not a number
  • DHawthorneDHawthorne Posts: 4,584
    I have an include file I put in all my projects that has lots of functions like the one GSLogic spelled out here. I strongly recommend you do the same - why re-write these little code snippets in every project?

    My original include was from one of the forum members here - the name on it in Inter (VCD). I have adapted and modified the heck out of it since, but you can still find the original here someplace.
  • Strings

    Thanks a lot for all the info... appreciated
  • jjamesjjames Posts: 2,908
    Not trying to get the last word in - I promise! But I noticed I forgot the other function that is needed in order for my second example to work. You'll also need this one:
    DEFINE_FUNCTION CHAR[2000] STRING_REPLACE(CHAR strSTR[], CHAR strSEARCH[], CHAR strREPLACE[])
    STACK_VAR
        INTEGER nPOS
        CHAR strTRASH[2000]
        CHAR strTEMP_STR[2000]
    {
        nPOS = FIND_STRING(strSTR, "strSEARCH", 1)
        IF (!nPOS)
            RETURN strSTR;
        WHILE (nPOS)
        {
            strTEMP_STR = ""
            IF (nPOS > 1)
                strTEMP_STR = LEFT_STRING(strSTR, nPOS - 1)
            strTRASH = REMOVE_STRING(strSTR, "strTEMP_STR, strSEARCH", 1)
            strSTR = "strTEMP_STR, strREPLACE, strSTR"
            nPOS = FIND_STRING(strSTR, "strSEARCH", nPOS + LENGTH_STRING(strREPLACE))
        }
        RETURN strSTR;
    }
    
    So, if you opt to use the second code example I posted, you'll also need to add this to the AXI file.

    Quick side note: since NS2 does not show functions properly when using #INCLUDE (a big gripe of mine), you can put any of your functions in the Netlinx.axi (C:\Program Files\Common Files\AMXShare\AXIs\Netlinx.axi) and it'll pop up everytime you start to type it. ;)
  • JJames,

    Thanks for posting this it helped me out with what I am currently working on :)
Sign In or Register to comment.