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 = []
while true
result.push foundPos
after = foundPos + s.len - 1
end while▼
end function
second = "jumped"
firstQ = """" + first + """" // (first string, in quotes)
secondQ = """" + second + """"
doesOrNot = [" does not ", " does "]
print
print
if
print firstQ + " does not contain " + secondQ + " anywhere"
else
print firstQ + "
end for
end if
print
print
▲ if start == null then print "No"
▲ while start != null
▲ start = first.indexOf(second, start)
▲ end while
{{out}}
<pre>
▲Does "The brown dog jumped jumped and jumped" start with "jumped"?
"The brown dog jumped jumped and jumped" is found at position 32 in "jumped"
▲Does "The brown dog jumped jumped and jumped" end with "jumped"?
</pre>
|