AMX REPLACE_STRING
Frequently Asked Questions
Brand:
- AMX
Models:
- Netlinx
Question:
Is there a way to replace specific characters, or remove specific characters, from a string?
Answer:
Download:
See REPLACE_STRING.axi located in the download:
Code Block:
PROGRAM_NAME='REPLACE_STRING'
(***********************************************************)
(***********************************************************)
(* 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
FSR_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[FSR_MAX_STRING_LENGTH] REPLACE_STRING(CHAR strSTR[],CHAR strSEARCH[],CHAR strREPLACE[])
(************************************************************************************************)
(* REPLACE_STRING FUNCTION *)
(* Search and replace characters in a string *)
(* The syntax: *)
(* *)
(* CHAR[MAX_STRING_LENGTH] REPLACE_STRING(CHAR strSTR[],CHAR strSEARCH[],CHAR strREPLACE[]) *)
(* *)
(* PARAMETERS: *)
(* strSTR = string variable *)
(* strSEARCH = string to search for *)
(* strREPLACE = string to replace strSEARCH *)
(* NOTE: To remove characters from the string pass '' in to strREPLACE *)
(* *)
(* RESULT: *)
(* A string containing the replaced characters. *)
(* *)
(* EXAMPLES: *)
(* strVAR = 'ABC ABC ABC' *)
(* strRESULT = REPLACE_STRING(strVAR,'ABC','CBA') *)
(* RESULT: *)
(* strRESULT is 'CBA CBA CBA' *)
(* *)
(* strVAR = 'ABCD ABCD ABCD' *)
(* strRESULT = REPLACE_STRING(strVAR,'C','') *)
(* RESULT: *)
(* strRESULT is 'ABD ABD ABD' *)
(* *)
(* 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[FSR_MAX_STRING_LENGTH]
CHAR strTEMP_HEADER[FSR_MAX_STRING_LENGTH]
CHAR strTRASH[255]
LONG nPOS
LONG nOLDPOS
{
strTEMP = strSTR //use a temporary string to keep integrity of original string
nPOS = FIND_STRING(strTEMP,strSEARCH,1) //begin searching for string to replace
IF (!nPOS) //search found no matching string
{
RETURN strSTR //return original string
}
WHILE(nPOS) //the search string is in the string
{
strTEMP_HEADER = LEFT_STRING(strTEMP,nPOS-1) //get the first part of the string
strTRASH = REMOVE_STRING(strTEMP,strSEARCH,1) //get rid of the first part of the string
strTEMP = "strTEMP_HEADER,strREPLACE,strTEMP" //put string back together and insert strREPLACE
nOLDPOS = nPOS+LENGTH_STRING(strREPLACE) //save old position + length of the strREPLACE
nPOS = FIND_STRING(strTEMP,strSEARCH,nOLDPOS) //continue searching string to replace, start at the position after the last replaced string
}
RETURN strTEMP //return the complete string with the replacement characters
}
(***********************************************************)
(* 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 !!! *)
(* *)
(*****************************************************************)