String matching: Difference between revisions

Content added Content deleted
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'

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\==0 then say 'string A contains string B (starting in position' p")"
if p==0 then say "string A doesn't contain string B"
else 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'

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 forever until p==0
Ps=''; p=0; do until p==0
p = pos(b, a, p+1)
p=pos(b, a, p+1)
if p\==0 then Ps = Ps',' p
if p\==0 then Ps = Ps',' p
end /*forever until p==0*/
end /*until ···*/
Ps = space(strip(Ps,'L',","))
Ps=space(strip(Ps, 'L', ","))
times = words(Ps)
times=words(Ps)
if times\==0 then say 'string A contains string B',
if times==0 then say "string A doesn't contain string B"
times 'time'left('s',times>1),
else say 'string A contains string B ',
"(at position"left('s',times>1) Ps')'
times 'time'left('s',times>1),
else say "string A doesn't contains string B"
"(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): &nbsp; <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: &nbsp; <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: &nbsp; <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 doesn't end with string B
string A contains string B 1 time (at position 1)
</pre>

'''output''' when the following is specified: &nbsp; <tt> Chico_Harpo_Groucho_Zeppo_Gummo mmo </tt>
string A contains string B 1 time (at position 1)
</pre>
<pre>
'''output''' when the following is specified: <tt> Chico_Harpo_Groucho_Zeppo_Gummo mmo </tt>
string A = Chico_Harpo_Groucho_Zeppo_Gummo
string B = mmo
<pre style="overflow:scroll">
string A= Chico_Harpo_Groucho_Zeppo_Gummo
string B= mmo


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>