|
String Comparing and Searching |
Description:
Sheerpower includes many ways to search strings and compare them.
Like all other string functions, they are highly optimized.
All comparison functions return a numeric result.
Importance in Business Applications:
COMPARE(), MATCH(),
and POS() allow businesses to:
Description:
The COMPARE() function compares two strings and returns
a numeric value from 0 (no match) to 100
(an exact match). It uses an enhanced matching algorithm that
understands common typographical and keyboarding errors (for
example, nearby keys on a QWERTY keyboard or hands on the wrong
row), as well as certain spelling and phonetic variations
(such as F vs PH).
This makes COMPARE() more "human aware" than a simple
edit-distance. General guidelines for interpreting scores:
90–100 — Very strong match (usually the
same word or name).80–89 — Good match (minor typos or
variants).70–79 — Possible match (often needs
review).70 — Typically a weak match.Example:
Output: Did you mean DOWN (94 percent)
Description:
CONTAINS() returns TRUE if
str_expr1 contains str_expr2, otherwise
FALSE. By default the comparison is case-insensitive.
Case sensitivity:
Include an optional third argument (bool_expr).
Pass TRUE for a case-sensitive match; omit it or pass
FALSE for case-insensitive.
Examples:
Output:
This is a text file
Notes:
str_expr2 is longer than str_expr1,
CONTAINS() returns FALSE.TRUE
(every string contains the empty string).Description:
ENDSWITH() returns TRUE if
str_expr1 ends with str_expr2, otherwise
FALSE. By default the comparison is case-insensitive.
Case sensitivity:
Include an optional third argument (bool_expr).
Pass TRUE for a case-sensitive match; omit it or pass
FALSE for case-insensitive.
Examples:
Output:
This is a text file
Notes:
str_expr2 is longer than str_expr1,
ENDSWITH() returns FALSE.TRUE
(every string ends with the empty string).Description:
The ITEM() function returns the number of the
first item that matches the whole or partial item name given.
A negative number is returned if more than one item matches.
Example:
z = item('ADD,EXIT,EXTRACT,MODIFY', 'MOD')
Output: 4
z = item('ADD,EXIT,EXTRACT,MODIFY', 'EX')
Output: -2
Description:
The MATCH() function compares str_expr2 with
each of the elements in str_expr1 and returns the number of
the element that matches. The TRUE parameter is used if the
match is to be case-exact. The default is case-insensitive.
Example:
a$ = 'BLUE'
b$ = 'Blue'
c$ = a$
do
x = match('Red,White,Blue', c$, true)
print c$;
if x > 0 then
print ' is a match.'
else
print ' is not a match.'
c$ = b$
repeat do
end if
end do
Output: Blue is a match.
Description:
The MATCHWORD() function searches str_expr1
for the word or phrase given in str_expr2 and returns
the 1-based character position of the first match. The search is
case-insensitive ("case-regardless"). Words are made up of letters
and/or numbers; non-alphanumeric characters act as separators.
Multiple spaces between words are ignored. If the word or phrase is
not found, MATCHWORD() returns 0.
After a successful call, the special variable _integer
contains the 1-based word index of the match within
str_expr1.
The optional num_expr argument specifies the starting
character position for the search within str_expr1.
If omitted, the default starting position is 1.
Example:
text$ = 'list of words or 11111 numbers'
print matchword(text$, 'Words')
print _integer
end
Output:
14
3
In this example, MATCHWORD() returns 14,
the starting character position of the word "words" in
text$. The variable _integer is set to
3, because "words" is the third word in the string.
Description:
The PATTERN() function matches characters in
str_expr1 against a pattern specified by
str_expr2. It returns the position of the first character in
str_expr1 that matches the pattern. If no match is found, it
returns zero.
Pattern Options:
{a-z}) or individual characters.
Example:
str_expr1
against enclosed groups.
Example:
Description:
The POS() function searches for a substring within a string
and returns the position of the first character of the substring. If the
substring is not found, the function returns zero.
Example:
If int_expr is provided and is positive, the search starts at
the int_expr position from the left. If int_expr
is negative, the search is performed from right to left, beginning at the
-int_expr position.
text$ = "Hello and goodbye"andpos = pos(text$, 'and')if andpos > 0 then print 'AND found at position'; andposDescription:
For a detailed description, see:
Regular Expression Text Search
Arguments:
text_str (required): The text to operate on.
expr_str: The regular expression search with.
Example:
print regexsearch('The rain in Spain', 'rain')
Output: 5
Description:
The SCAN() function scans str_expr1 for the
characters in str_expr2 and returns the position at which
str_expr2 begins. The characters in str_expr2
need not appear contiguously in str_expr1.
Example:
a$ = 'Cameron Whitehorn'
b$ = 'amr Wtor'
position = scan(a$, b$)
Output: 2
Description:
The SKIP() function returns the position of the character
following the last skipped character.
Example:
a$ = '31415 hello'
z = skip(a$, '1234567890 ')
print mid(a$, z)
Output: hello
Description:
The STARTSWITH() function tests whether one string begins with another.
It returns TRUE if the starting characters match, or FALSE if not.
By default, the comparison is not case-sensitive.
To perform a case-sensitive comparison, include an optional third argument
and set it to TRUE.
Examples:
filename$ = "Report_Final.TXT"if startswith(filename$, "report") then print "Starts with 'report'"if startswith(filename$, "Report", TRUE) then print "Exact case match"Output:
Starts with 'report'
Notes:
str_expr2 is longer than str_expr1,
STARTSWITH() returns FALSE.TRUE
(every string begins with an empty string).|
Hide Description
|
|
|
Enter or modify the code below, and then click on RUN |
|
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use. |