Tech Tip: How to strip out the trailing spaces from text
PRODUCT: 4D | VERSION: 11.6 | PLATFORM: Mac & Win
Published On: April 9, 2010
Here is a quick snippet of code that can be used to strip out any trailing spaces from text:
C_TEXT($1;$string_t) C_TEXT($0) C_BOOLEAN($continue_b) C_LONGINT($tmpLength_l;$lastCharFound_l) C_TEXT($tmpTestThisChar_t) $string_t:=$1 $continue_b:=True Repeat $tmpLength_l:=Length($string_t) $lastCharFound_l:=Position(" ";$string_t;$tmpLength_l) $tmpTestThisChar_t:=Substring($string_t;$lastCharFound_l) If ($tmpTestThisChar_t=" ") $string_t:=Substring($string_t;1;$lastCharFound_l-1) Else $continue_b:=False End if Until ($continue_b=False) $0:=$string_t |
Although it is less easy to read, the above code can be shortened by a few lines by removing the $tempLength_l, $lastCharFound_l, $tmpTestThisChar_t, and $continue_b variables from the code. Here is an example:
C_TEXT($1;$string_t) C_TEXT($0) $string_t:=$1 Repeat If (Substring($string_t;Position(" ";$string_t;Length($string_t)))=" ") $string_t:=Substring($string_t;1;Position(" ";$string_t;Length($string_t))-1) End if Until (Substring($string_t;Position(" ";$string_t;Length($string_t)))#" ") $0:=$string_t |
If the above code is saved as a project method named "StripTrailingSpaces" an example call would be:
C_TEXT($return) $return:=StripTrailingSpaces("Some Test With Trailing Spaces! ") |
After calling this method $return would then equal "Some Test With Trailing Spaces!"