Tech Tip: Utility method to check if a string starts with another string
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: February 27, 2023
The method below determines whether or not the beginning of a string matches another string. The method is case-sensitive.
/* METHOD: stringStartsWith INPUT: 1st string to check the start of; 2nd string to compare with OUTPUT: True if 1st string starts with 2nd string, otherwise False NOTE: string comparison is case-sensitive */ #DECLARE($src : Text; $txt : Text)->$res : Boolean var $str : Text var $val : Integer If (Count parameters=2) If (Length($txt)>Length($src)) $res:=False Else $str:=Substring($src; 1; Length($txt)) $val:=Compare strings($str; $txt; sk strict) If ($val=0) $res:=True Else $res:=False End if End if End if |
Examples:
stringStartsWith("Mr. Smith"; "Mr.") // True stringStartsWith("Mr. Smith"; "mr.") // False |