String matching: Difference between revisions
Content added Content deleted
Walterpachl (talk | contribs) m (→{{header|REXX}}: typo (contains -> contain) |
m (→{{header|REXX}}: corrected a spelling error, added comments and more whitespace within literals, use "true" IFs, removed FOREVER from DO statement.) |
||
Line 1,994: | Line 1,994: | ||
Extra coding was added to take care of using plurals in the output messages. |
Extra coding was added to take care of using plurals in the output messages. |
||
<lang rexx>/*REXX program demonstrates some basic character string testing. */ |
<lang rexx>/*REXX program demonstrates some basic character string testing. */ |
||
parse arg a b /*obtain A and B from the C.L. */ |
|||
parse arg a b |
|||
say 'string A=' a |
say 'string A = ' a /*display string A to terminal.*/ |
||
say 'string B=' b |
say 'string B = ' b /* " " B " " */ |
||
say |
say |
||
if left(A,length(b))==b then say 'string A starts with string B' |
|||
else say "string A doesn't start with string B" |
|||
else say "string A doesn't start with string B" |
|||
say |
say |
||
/*another method, however a wee bit obtuse. */ |
/*another method, however a wee bit obtuse. */ |
||
/*¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬ |
/*¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬ |
||
Line 2,008: | Line 2,006: | ||
else say "string A doesn't start with string B" |
else say "string A doesn't start with string B" |
||
¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬*/ |
¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬*/ |
||
/* [↑] above is a big comment. */ |
|||
p=pos(b,a) |
p=pos(b,a) |
||
if p |
if p==0 then say "string A doesn't contain string B" |
||
else say 'string A contains string B (starting in position' p")" |
|||
say |
say |
||
if right(A,length(b))==b then say 'string A ends with string B' |
|||
else say "string A doesn't end with string B" |
|||
else say "string A doesn't end with string B" |
|||
say |
say |
||
Ps=''; p=0; do |
Ps=''; p=0; do until p==0 |
||
p |
p=pos(b, a, p+1) |
||
if p\==0 then Ps = Ps',' p |
if p\==0 then Ps = Ps',' p |
||
end /* |
end /*until ···*/ |
||
Ps |
Ps=space(strip(Ps, 'L', ",")) |
||
times |
times=words(Ps) |
||
if times |
if times==0 then say "string A doesn't contain string B" |
||
else say 'string A contains string B ', |
|||
times 'time'left('s',times>1), |
|||
"(at position"left('s', times>1) Ps')' |
|||
/*stick a fork in it, we're done.*/</lang> |
/*stick a fork in it, we're done.*/</lang> |
||
'''output''' when the following is specified (five Marx brothers): <tt> Chico_Harpo_Groucho_Zeppo_Gummo p </tt> |
'''output''' when the following is specified (five Marx brothers): <tt> Chico_Harpo_Groucho_Zeppo_Gummo p </tt> |
||
<pre> |
|||
<pre style="overflow:scroll"> |
|||
string A= Chico_Harpo_Groucho_Zeppo_Gummo |
string A = Chico_Harpo_Groucho_Zeppo_Gummo |
||
string B= p |
string B = p |
||
string A doesn't start with string B |
string A doesn't start with string B |
||
string A contains string B (starting in position 10) |
string A contains string B (starting in position 10) |
||
string A doesn't end with string B |
string A doesn't end with string B |
||
string A contains string B 3 times (at positions 10, 23, 24) |
string A contains string B 3 times (at positions 10, 23, 24) |
||
</pre> |
</pre> |
||
'''output''' when the following is specified: <tt> Chico_Harpo_Groucho_Zeppo_Gummo Z </tt> |
'''output''' when the following is specified: <tt> Chico_Harpo_Groucho_Zeppo_Gummo Z </tt> |
||
<pre> |
|||
<pre style="overflow:scroll"> |
|||
string A= Chico_Harpo_Groucho_Zeppo_Gummo |
string A = Chico_Harpo_Groucho_Zeppo_Gummo |
||
string B= Z |
string B = Z |
||
string A doesn't start with string B |
string A doesn't start with string B |
||
string A contains string B (starting in position 21) |
string A contains string B (starting in position 21) |
||
string A doesn't end with string B |
string A doesn't end with string B |
||
string A contains string B 1 time (at position 21) |
string A contains string B 1 time (at position 21) |
||
</pre> |
</pre> |
||
'''output''' when the following is specified: <tt> Chico_Harpo_Groucho_Zeppo_Gummo Chi </tt> |
'''output''' when the following is specified: <tt> Chico_Harpo_Groucho_Zeppo_Gummo Chi </tt> |
||
<pre> |
|||
<pre style="overflow:scroll"> |
|||
string A= Chico_Harpo_Groucho_Zeppo_Gummo |
string A = Chico_Harpo_Groucho_Zeppo_Gummo |
||
string B= Chi |
string B = Chi |
||
string A starts with string B |
string A starts with string B |
||
string A contains string B (starting in position 1) |
string A contains string B (starting in position 1) |
||
string A doesn't end with string B |
|||
string A |
string A contains string B 1 time (at position 1) |
||
</pre> |
|||
'''output''' when the following is specified: <tt> Chico_Harpo_Groucho_Zeppo_Gummo mmo </tt> |
|||
string A contains string B 1 time (at position 1) |
|||
< |
<pre> |
||
string A = Chico_Harpo_Groucho_Zeppo_Gummo |
|||
⚫ | |||
<pre style="overflow:scroll"> |
|||
string A= Chico_Harpo_Groucho_Zeppo_Gummo |
|||
⚫ | |||
string A doesn't start with string B |
string A doesn't start with string B |
||
string A contains string B (starting in position 29) |
string A contains string B (starting in position 29) |
||
string A ends with string B |
string A ends with string B |
||
string A contains string B 1 time (at position 29) |
string A contains string B 1 time (at position 29) |
||
</pre> |
</pre> |
||