String matching: Difference between revisions

reworked MiniScript example
(reworked MiniScript example)
Line 2,384:
 
=={{header|MiniScript}}==
We first extend the built-in string class with three new methods, and then demonstrate their use on some sample strings.
<lang MiniScript>first = "The brown dog jumped jumped and jumped"
 
<lang MiniScript>string.startsWith = function(s)
return self.len >= s.len and s[:s.len] == s
end function
 
string.endsWith = function(s)
return self.len >= s.len and s[-s.len:] == s
end function
 
string.findAll = function(s)
result = []
whileafter start != null
while true
startfoundPos = firstself.indexOf(seconds, startafter)
if startfoundPos == null then printreturn "No"result
result.push foundPos
after = foundPos + s.len - 1
end while
end function
<lang MiniScript>first = "The brown dog jumped jumped and jumped"
second = "jumped"
 
firstQ = """" + first + """" // (first string, in quotes)
location = first.indexOf(second)
secondQ = """" + second + """"
lastLocation = location
doesOrNot = [" does not ", " does "]
 
print "Does """firstQ + doesOrNot[first.startsWith(second)] + """ start with """ + second + """?"secondQ
print
if location == 0 then print "Yes" else print "No"
 
printfound "Does """ += first + """ contain """ + .findAll(second + """?")
if locationnot == nullfound then
print firstQ + " does not contain " + secondQ + " anywhere"
print "No"
else
printfor "Yes"pos in found
print firstQ + "Match is found at position =" + pos + " in " + first.indexOf(second)secondQ
end for
end if
print
 
print "Does """firstQ + doesOrNot[first.endsWith(second)] + """end contain multiple references towith """ + second + """?"secondQ</lang>
if location == null then
print "No"
else
start = first.indexOf(second, location)
if start == null then print "No"
while start != null
lastLocation = start
print "Match at location " + start
start = first.indexOf(second, start)
end while
end if
 
print "Does """ + first + """ end with """ + second + """?"
if lastLocation == first.len - second.len then print "Yes" else print "no"
</lang>
{{out}}
<pre>
Does "The brown dog jumped jumped and jumped" does start with "jumped"?
 
Does "The brown dog jumped jumped and jumped" start with "jumped"?
Does "The brown dog jumped jumped and jumped" endis found at position 14 within "jumped"?
No
Does "The brown dog jumped jumped and jumped" containis found at position 21 in "jumped"?
"The brown dog jumped jumped and jumped" is found at position 32 in "jumped"
Yes
Match position = 14
Does "The brown dog jumped jumped and jumped" contain multipledoes referencesend towith "jumped"?
Match at location 21
Match at location 32
Does "The brown dog jumped jumped and jumped" end with "jumped"?
Yes
</pre>
 
222

edits