![]() |
String Comparing and Searching |
Description:
Sheerpower includes many ways to search strings and compare them.
Like all other string functions, they are highly optimized.
Importance in Business Applications:
COMPARE()
, MATCH()
,
and POS()
allow businesses to:
Description:
The COMPARE()
function compares two strings
and returns a numeric value ranging from 0 (no match) to 100
(an exact match).
Example:
Output: Did you mean DOWN (94 percent)
Description:
The ENDSWITH()
function tests whether one string ends with another.
It returns TRUE
if the ending matches, 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:
filespec$ = "report_final.TXT"
if endswith(filespec$, ".txt") then print "This is a text file"
if endswith(filespec$, ".TXT", TRUE) then print "Exact case match"
Output:
This is a text file
Notes:
str_expr2
is longer than str_expr1
,
ENDSWITH()
returns FALSE
.TRUE
(every string ends with an 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 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'; andpos
Description:
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. |