Home AMX User Forum NetLinx Studio

How to remove the first or last space from a string

For example, I have a string str= ' this is a string ', there are spaces at the beginning and the end of this string, how can I remove it in Netlinx studio?

Thanks

Comments

  • AuserAuser Posts: 506
    Depending on your application, here's something I just cranked out (untested). You'll need to define MAX_WIDTH_STRING.
    DEFINE_CONSTANT
    #IF_NOT_DEFINED HT
      HT = $09
    #END_IF
    
    // Name   : ==== Trim Left ====
    // Purpose: Trim leading spaces from a string
    // Params : (1) IN/OUT  - char inputdata[]/char formatteddata[]
    // Returns: Char array representing the input string with leading spaces removed
    // Notes  :
    //
    define_function char[MAX_WIDTH_STRING] TrimLeft(char _cInputString[])
    {
      stack_var integer  _nResultStartPos
      
      _nResultStartPos = 1
      while
      (
        (_nResultStartPos <= length_array(_cInputString)) &&
        (
          (_cInputString[_nResultStartPos] = ' ') ||
          (_cInputString[_nResultStartPos] = "HT")
        )
      )
      {
        _nResultStartPos++
      }
      if(_nResultStartPos <= length_array(_cInputString))
        return right_string(_cInputString, length_array(_cInputString) - _nResultStartPos + 1)
      else
        return ''
    }
    
    // Name   : ==== Trim Right ====
    // Purpose: Trim trailing spaces from a string
    // Params : (1) IN/OUT  - char inputdata[]/char formatteddata[]
    // Returns: Char array representing the input string with leading spaces removed
    // Notes  :
    //
    
    define_function char[MAX_WIDTH_STRING] TrimRight(char _cInputString[])
    {
      stack_var integer  _nResultStartPos
      
      _nResultStartPos = length_array(_cInputString)
      while
      (
        (_nResultStartPos) &&
        (
          (_cInputString[_nResultStartPos] == ' ') ||
          (_cInputString[_nResultStartPos] == "HT")
        )
      )
      {
        _nResultStartPos--
      }
      if(_nResultStartPos)
        return left_string(_cInputString, _nResultStartPos)
      else
        return ''
    }
    
    // Name   : ==== Trim ====
    // Purpose: Trim leading and trailing spaces from a string
    // Params : (1) IN/OUT  - char inputdata[]/char formatteddata[]
    // Returns: Char array representing the input string with leading and trailing spaces removed
    // Notes  :
    //
    define_function char[MAX_WIDTH_STRING] Trim(char _cInputString[])
    {
      stack_var char _cResultString[MAX_WIDTH_STRING]
      // Remove leading spaces from the string
      _cResultString = TrimLeft(_cInputString)
    
      // Remove trailing spaces from the string
      _cResultString = TrimRight(_cResultString)
    
      return _cResultString
    }
    
    
  • yanbinyanbin Posts: 86
    Auser wrote: »
    Depending on your application, here's something I just cranked out (untested). You'll need to define MAX_WIDTH_STRING.
    // Name   : ==== Trim Left ====
    // Purpose: Trim leading spaces from a string
    // Params : (1) IN/OUT  - char inputdata[]/char formatteddata[]
    // Returns: Char array representing the input string with leading spaces removed
    // Notes  :
    //
    define_function char[MAX_WIDTH_STRING] TrimLeft(char _cInputString[])
    {
      stack_var integer  _nResultStartPos
      
      _nResultStartPos = 1
      while
      (
        (_nResultStartPos <= length_array(_cInputString)) &&
        (
          (_cInputString[_nResultStartPos] = ' ') ||
          (_cInputString[_nResultStartPos] = "HT")
        )
      )
      {
        _nResultStartPos++
      }
      if(_nResultStartPos <= length_array(_cInputString))
        return right_string(_cInputString, length_array(_cInputString) - _nResultStartPos + 1)
      else
        return ''
    }
    
    // Name   : ==== Trim Right ====
    // Purpose: Trim trailing spaces from a string
    // Params : (1) IN/OUT  - char inputdata[]/char formatteddata[]
    // Returns: Char array representing the input string with leading spaces removed
    // Notes  :
    //
    
    define_function char[MAX_WIDTH_STRING] TrimRight(char _cInputString[])
    {
      stack_var integer  _nResultStartPos
      
      _nResultStartPos = length_array(_cInputString)
      while
      (
        (_nResultStartPos) &&
        (
          (_cInputString[_nResultStartPos] == ' ') ||
          (_cInputString[_nResultStartPos] == "HT")
        )
      )
      {
        _nResultStartPos--
      }
      if(_nResultStartPos)
        return left_string(_cInputString, _nResultStartPos)
      else
        return ''
    }
    
    // Name   : ==== Trim ====
    // Purpose: Trim leading and trailing spaces from a string
    // Params : (1) IN/OUT  - char inputdata[]/char formatteddata[]
    // Returns: Char array representing the input string with leading and trailing spaces removed
    // Notes  :
    //
    define_function char[MAX_WIDTH_STRING] Trim(char _cInputString[])
    {
      stack_var char _cResultString[MAX_WIDTH_STRING]
      // Remove leading spaces from the string
      _cResultString = TrimLeft(_cInputString)
    
      // Remove trailing spaces from the string
      _cResultString = TrimRight(_cResultString)
    
      return _cResultString
    }
    
    


    Thanks Auser!
  • yanbinyanbin Posts: 86
    Also if I want to send text to multiple buttons, for example, if I want to sent text 'CD' to variable text channel 1,2,3,5 with one command, , how can I do it?

    Will this command work? send_command TP,"'TEXT1,2,3,5-CD'"
  • AuserAuser Posts: 506
    yanbin wrote: »
    Thanks Auser!

    I've just updated that code snippet to include a definition for the horizontal tab ASCII character which I had inadvertently omitted.
  • viningvining Posts: 4,368
    From PI
    "'^TXT-<variable text address range>,<button states range>,<new text>'"

    Assign a text string to those buttons with a defined address range.

    Sets non-unicode text.



    Syntax:

    SEND_COMMAND <DEV>,"'^TXT-<vt addr range>,<button states range>,<new text>'"



    Variables:

    variable text address range = 1 - 4000.

    button states range = 1 - 256 for multi-state buttons

    (0 = All states, for General buttons 1 = Off state and 2 = On state).

    new text = 1 - 50 ASCII characters.



    Example:

    SEND_COMMAND Panel,"'^TXT-500.504&510.515,1&2,Test Only'"

    Sets the On and Off state text for buttons with the variable

    text ranges of 500-504 & 510-515.
    In the above example "Test Only" will be sent to buttons with a variable text address of 500 to 504 and to 510 through 515.

    BMF can do this too (I think) but the ^TXT is a bit simpler.
  • yanbinyanbin Posts: 86
    vining wrote: »
    From PI

    In the above example "Test Only" will be sent to buttons with a variable text address of 500 to 504 and to 510 through 515.

    BMF can do this too (I think) but the ^TXT is a bit simpler.

    So Send-command tp,"'^TXT-1&2&3&5,1&2,Test only'" will send text to button 1,2,3 and 5?
  • viningvining Posts: 4,368
    yanbin wrote: »
    So Send-command tp,"'^TXT-1&2&3&5,1&2,Test only'" will send text to button 1,2,3 and 5?
    Well off the top of my head I don't how many times you can use "&" but it might work as you described.

    I would write it like this:
    So Send-command tp,"'^TXT-1.3&5,0,Test only'" will send text to button 1,2,3 and 5? 
    
    I would use the . (dot) operator so 1 through 3 (1.3) and 5 (&5) and for states 0 equals states 1 & 2.
  • jjamesjjames Posts: 2,908
    FYI

    Question: Is there a way to remove the spaces before or after characters in a string?
    Answer: TRIM_STRING

    Question: Is there a way to search for characters in a string starting at the end?
    Answer: FIND_STRING_REV

    Question: Is there a way to replace specific characters, or remove specific characters, from a string?
    Answer: REPLACE_STRING

    Question: Is there a way to assign sub-strings separated by delimiters from a string to an array?
    Answer: SPLIT_STRING

    Question: Is there a way to count the number of times a pattern of characters occurs in a string?
    Answer: COUNT_STRING
  • AuserAuser Posts: 506
    Huh, I never new those tech notes existed. My old implementation of trim used the while(whitespace): left_string()/right_string() construct used in the tech note. When I looked at it yesterday when replying to the OP I realised it's horribly inefficient, hence why I hammered out the version I posted. In the tech note method, for every character of whitespace you've got a string copy operation. Depending on the amount of white space and the size of the string that could be very time consuming...
  • PhreaKPhreaK Posts: 966
    You may also find trim(), ltrim() and rtrim() useful - they're all in the NetLinx Common Libraries string library.
  • yanbinyanbin Posts: 86
    vining wrote: »
    Well off the top of my head I don't how many times you can use "&" but it might work as you described.

    I would write it like this:
    So Send-command tp,"'^TXT-1.3&5,0,Test only'" will send text to button 1,2,3 and 5? 
    
    I would use the . (dot) operator so 1 through 3 (1.3) and 5 (&5) and for states 0 equals states 1 & 2.

    the variable text channels are random, they can be 1,3,5,7,10, how to send text to them?
  • viningvining Posts: 4,368
    yanbin wrote: »
    the variable text channels are random, they can be 1,3,5,7,10, how to send text to them?
    Try it with the multiple "&" and see if that works, it may. I've never tried to use multiples so I can't say what will happen. If it doesn't then you're back at square one but if it does your golden. Either way let us know cuz I am curious.
  • yanbinyanbin Posts: 86
    vining wrote: »
    Try it with the multiple "&" and see if that works, it may. I've never tried to use multiples so I can't say what will happen. If it doesn't then you're back at square one but if it does your golden. Either way let us know cuz I am curious.

    I tested it with AXD-CV6 and a MVP5200i, send_command tp,"'^TXT-1&3&5&7&10,0,Text Only'" work in MVP5200i but not AXD-CV6, I checked the AXD-CV6 manual, could not find any multiple send text command.
  • jjamesjjames Posts: 2,908
    AXD-CV6 = G3
    MVP5200i = G4

    ^TXT is a G4 command

    Aren't the differences between G3 and G4 panels distinguished well enough in the course of AMX training? If not - it should be.
  • yanbinyanbin Posts: 86
    jjames wrote: »
    AXD-CV6 = G3
    MVP5200i = G4

    ^TXT is a G4 command

    Aren't the differences between G3 and G4 panels distinguished well enough in the course of AMX training? If not - it should be.

    I realized the difference between G3 and G4 panels, I just want to find a command can do both G3 and G4 panels.
Sign In or Register to comment.