AMX TRIM_STRING
Technical Support Guide
Table of Contents
Brand:
- AMX
Models:
- Netlink
Question:
- Is there a way to remove the spaces before or after characters in a string?
Answer:
Code Block
PROGRAM_NAME='TRIMSTRING'
(***********************************************************)
(***********************************************************)
(* FILE_LAST_MODIFIED_ON: 04/05/2006 AT: 09:00:25 *)
(***********************************************************)
(* System Type : NetLinx *)
(***********************************************************)
(* REV HISTORY: *)
(***********************************************************)
(*
$History: $
*)
(***********************************************************)
(* DEVICE NUMBER DEFINITIONS GO BELOW *)
(***********************************************************)
DEFINE_DEVICE
(***********************************************************)
(* CONSTANT DEFINITIONS GO BELOW *)
(***********************************************************)
DEFINE_CONSTANT
TRIMSTRING_MAX_STRING_LENGTH = 1000
(***********************************************************)
(* DATA TYPE DEFINITIONS GO BELOW *)
(***********************************************************)
DEFINE_TYPE
(***********************************************************)
(* VARIABLE DEFINITIONS GO BELOW *)
(***********************************************************)
DEFINE_VARIABLE
(***********************************************************)
(* LATCHING DEFINITIONS GO BELOW *)
(***********************************************************)
DEFINE_LATCHING
(***********************************************************)
(* MUTUALLY EXCLUSIVE DEFINITIONS GO BELOW *)
(***********************************************************)
DEFINE_MUTUALLY_EXCLUSIVE
(***********************************************************)
(* SUBROUTINE/FUNCTION DEFINITIONS GO BELOW *)
(***********************************************************)
(* EXAMPLE: DEFINE_FUNCTION <RETURN_TYPE> <NAME> (<PARAMETERS>) *)
(* EXAMPLE: DEFINE_CALL '<NAME>' (<PARAMETERS>) *)
DEFINE_FUNCTION CHAR[TRIMSTRING_MAX_STRING_LENGTH] TRIM_STRING(strSTR[],strDIR[2])
(****************************************************************************************************************)
(* TRIM_STRING FUNCTION *)
(* Remove leading and/or trailing spaces in a string *)
(* The syntax: *)
(* *)
(* CHAR[MAX_STRING_LENGTH] TRIM_STRING(strSTR[],strDIR[2]) *)
(* *)
(* PARAMETERS: *)
(* strSTR = string variable *)
(* strDIR = which end the spaces should be removed from the string *)
(* NOTE: To remove spaces from the left pass 'L' in to strDIR. *)
(* To remove spaces from the right pass 'R' in to strDIR. *)
(* To remove spaces from both ends pass 'LR'. *)
(* *)
(* RESULT: *)
(* A string containing the removed spaces. *)
(* *)
(* EXAMPLES: *)
(* strVAR = ' ABC ABC ABC ' *)
(* strRESULT = TRIM_STRING(strVAR,'LR') *)
(* RESULT: *)
(* strRESULT is 'ABC ABC ABC' *)
(* *)
(* strVAR = ' ABC ABC ABC ' *)
(* strRESULT = TRIM_STRING(strVAR,'L') *)
(* RESULT: *)
(* strRESULT is 'ABC ABC ABC ' *)
(* *)
(* strVAR = ' ABC ABC ABC ' *)
(* strRESULT = TRIM_STRING(strVAR,'R') *)
(* RESULT: *)
(* strRESULT is ' ABC ABC ABC' *)
(* *)
(* NOTE: *)
(* MAX_STRING_LENGTH must be defined in DEFINE_CONSTANT *)
(* specifying the maximum string length *)
(* *)
(* DEFINE_CONSTANT *)
(* MAX_STRING_LENGTH = 1000 *)
(* *)
(****************************************************************************************************************)
STACK_VAR CHAR strTEMP[TRIMSTRING_MAX_STRING_LENGTH]
{
strTEMP = strSTR //use a temporary string to keep integrity of original string
IF(FIND_STRING(UPPER_STRING(strDIR),'L',1)) //remove spaces from the left
{
WHILE(LEFT_STRING(strTEMP,1) = "' '") //loop until all spaces are removed
{
strTEMP = RIGHT_STRING(strTEMP,LENGTH_STRING(strTEMP)-1) //get rid of the space
}
}
IF(FIND_STRING(UPPER_STRING(strDIR),'R',1)) //remove spaces from the right
{
WHILE(RIGHT_STRING(strTEMP,1) = "' '") //loop until all spaces are removed
{
SET_LENGTH_STRING(strTEMP,LENGTH_STRING(strTEMP)-1) //get rid of the space
}
}
RETURN strTEMP //return the string w/out spaces
}
(***********************************************************)
(* STARTUP CODE GOES BELOW *)
(***********************************************************)
DEFINE_START
(***********************************************************)
(* THE EVENTS GO BELOW *)
(***********************************************************)
DEFINE_EVENT
(*****************************************************************)
(* *)
(* !!!! WARNING !!!! *)
(* *)
(* Due to differences in the underlying architecture of the *)
(* X-Series masters, changing variables in the DEFINE_PROGRAM *)
(* section of code can negatively impact program performance. *)
(* *)
(* See Ԅifferences in DEFINE_PROGRAM Program ExecutionԠsection *)
(* of the NX-Series Controllers WebConsole & Programming Guide *)
(* for additional and alternate coding methodologies. *)
(*****************************************************************)
DEFINE_PROGRAM
(*****************************************************************)
(* END OF PROGRAM *)
(* *)
(* !!! DO NOT PUT ANY CODE BELOW THIS COMMENT !!! *)
(* *)
(*****************************************************************)
Table of Contents