RosettaCodeData/Task/String-matching/REXX/string-matching.rexx

28 lines
2.1 KiB
Rexx
Raw Permalink Normal View History

2016-12-05 22:15:40 +01:00
/*REXX program demonstrates some basic character string testing (for matching). */
parse arg A B; LB=length(B) /*obtain A and B from the command line.*/
say 'string A = ' A /*display string A to the terminal.*/
say 'string B = ' B /* " " B " " " */
2017-09-23 10:01:46 +02:00
say copies('', 70)
2016-12-05 22:15:40 +01:00
if left(A, LB)==B then say 'string A starts with string B'
else say "string A doesn't start with string B"
say /* [↓] another method using COMPARE BIF*/
/*╔══════════════════════════════════════════════════════════════════════════╗
if compare(A,B)==LB then say 'string A starts with string B'
else say "string A doesn't start with string B"
*/
p=pos(B, A)
if p==0 then say "string A doesn't contain string B"
else say 'string A contains string B (starting in position' p")"
2015-02-20 09:02:09 -05:00
say
2016-12-05 22:15:40 +01:00
if right(A, LB)==b then say 'string A ends with string B'
else say "string A doesn't end with string B"
2015-02-20 09:02:09 -05:00
say
2017-09-23 10:01:46 +02:00
$=; p=0; do until p==0; p=pos(B, A, p+1)
if p\==0 then $=$',' p
end /*until*/
$=space(strip($, 'L', ",")) /*elide extra blanks and leading comma.*/
#=words($) /*obtain number of words in $ string.*/
2016-12-05 22:15:40 +01:00
if #==0 then say "string A doesn't contain string B"
2017-09-23 10:01:46 +02:00
else say 'string A contains string B ' # " time"left('s', #>1),
"(at position"left('s', #>1) $")" /*stick a fork in it, we're done*/