Tech Tip: Utility Method to Remove Trailing & Leading Character from String
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: January 31, 2022
This utility method helps remove any leading or trailing character from a string. This can be helpful for removing unwanted characters from the beginning and ends of a string (extra spaces, carriage returns, etc.). The first parameter takes in a string to modify and the second parameter lets the developer specify a character to remove if it is found in the beginning or end of the string.
//--------------------------------------------------------------- /* Method : Util_RemoveLTCharactersInString Description : This method removes leading and trailing characters from the string passed in $1. It removes the character passed in $2 Parameters : $1 (Text) - String to remove leading and trailing charcters from $2 (Text) - Character to remove from the beginnin and end of string $0 (Text) - String with leading and trailing characters removed */ //--------------------------------------------------------------- C_TEXT($0; $1; $2; $string_t; $toRemove) C_BOOLEAN($localBreak; $blockBreak) If (Count parameters=2) $string_t:=$1 $toRemove:=$2 Repeat $localBreak:=False Case of //remove the leading and trailing character passed in $2 : ($string_t=("@"+$toRemove)) | ($string_t=($toRemove+"@")) Repeat $blockBreak:=False Case of : ($string_t=("@"+$toRemove)) $string_t:=Delete string($string_t; Length($string_t)-Length($toRemove)+1; Length($toRemove)) : ($string_t=($toRemove+"@")) $string_t:=Delete string($string_t; 1; Length($toRemove)) : (True) $blockBreak:=True End case Until ($blockBreak) : (True) $localBreak:=True End case Until ($localBreak) $0:=$string_t End if |
In this example, the leading and trailing carriage return characters are removed.
$string_t:=Char(13)+"example"+Char(13)+"text"+Char(13)+Char(13) $returnString_t:=Util_RemoveLTCharactersInString($string_t; Char(13)) |