KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: A utility for prepending 4D escape "\\" on special characters
PRODUCT: 4D | VERSION: 14.0 | PLATFORM: Mac & Win
Published On: September 3, 2014

When working with many industry standard interfaces, such as the web, and applications, such as RegEx, Javascript, and JSON many character will have to be "escaped" to be considered none control characters. The utility below will accept a string of characters or strings and apply the 4D excape string "\\" to each parameter in the stirng.

// ----------------------------------------------------
// Method: UTIL_EscapeString
// Description
// This method will place \\ (escape) in front of all provide characters
// in the given string in $1
//
// Parameters
// $1 - Given String
// ${2} - Characters or string to be escaped
//
// Return
// $0 - Result String
// ----------------------------------------------------

C_TEXT($0;$1;${2};$String_T)
C_LONGINT($param_L;$i)

$param_L:=Count parameters
If ($param_L>1)
   $String_T:=$1

   For ($i;2;$param_L)
      If (Position(${$i};$String_T)>0)
         $String_T:=Replace string($String_T;${$i};"\\"+${$i})
      End if
   End for
End if

$0:=$String_T


Below is an example on how to use the utility.

$Str_T:="{Level_1.Level_2.Level_3}"
$Str_T:=UTIL_EscapeString ($Str_T;"?";".";"(";")";"{";"}";"[";"]";"*";"+")



$Str_T is now "\\{Level_1\\.Level_2\\.Level_3\\}." Note that the characters "{", "." and "}" have been escaped.

See Also: