The 4D language offers 20+ commands to handle strings. And with 4D v18 R6, 4D introduces a new command: Compare strings. And yet another command has been improved: Position. Both of these commands allow you to use options for more accurate results. Let’s learn more!
The well-known command, Position, enables finding the position of text within other text. It provides only two options: start position and exact match, based on character code. As of 4D v18 R6, you now have more comparison possibilities such as “case sensitive” or “accent sensitive”. The Japanese language has not been forgotten, either! Hiragana/Katakana and character width are also available as search options. There’s also the option to search for the “whole word”.
Let’s start from the beginning.
The first option is “strict”. It takes into account capitalization, accents, etc., as well as specificities of languages. In German, for example, “ß” can be assumed to be identical to “ss”. In French, œ, and æ are equivalent to and “oe” and “ae”.
The other available options speak for themselves. “case insensitive” and “accent insensitive” allow you to bypass accents and capital letters. For Japanese, “kana insensitive” ignores the differences between katakana and hiragana, and “width insensitive” ignores character widths.
The last (very useful) option allows ignoring parts of words to find only complete (whole) words.
A few examples
$p:=Position ("home";"I have done my homework";0;$length;sk whole word")
// returns zero ! home is not a whole word in this case.
$p:=Position ("Home";"I have done my homework";0;$length;sk case insensitive")
// returns 16; the capital "H" is considered equal to letter "h"
$p:=Position ("Joao";"I have a friend named João";0;$length;sk accent insensitive")
// returns 23; the accentuated letter "ã" is considered equal to letter "a"
These options can be cumulated!
$p:=Position ("joao";"I have a friend named João";0;$length;sk case insensitive + sk accent insensitive")
// also returns 23; the accentuated letter "ã" is considered equal to letter "a" AND letter "J" is considered equal to letter "j"
The new Compare strings command allows comparing strings with the same options as those added to the Position command. This command is based on the language defined in the database settings. This can be really useful to allow specific sorts, for instance.
The command will return:
- zero (0) if the strings are considered identical (based on criteria passed as options)
- a negative or positive value if the first string would be sorted before or after the second one.
A few examples
In the example below, $result returns “1” because “a” is considered bigger than “A”. Not by its size, but because of its character code!
Reminder: sk char code makes the comparison independent of the language.
$result:=Compare strings("alpha"; "Alpha"; sk char codes)
This time, $result returns “0” because lower and upper cases must be considered as identical, thanks to the sk case insensitive option.
Reminder: sk case insensitive makes the comparison dependent on the language.
$result:=Compare strings("a tree"; "A tree"; sk case insensitive)
Have a look at the HDI above and see how these new options can be used.
And make sure to join the discussion on the 4D forum!