RosettaCodeData/Task/String-matching/MiniScript/string-matching.mini

41 lines
1.0 KiB
Plaintext

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 = []
after = null
while true
foundPos = self.indexOf(s, after)
if foundPos == null then return result
result.push foundPos
after = foundPos + s.len - 1
end while
end function
first = "The brown dog jumped jumped and jumped"
second = "jumped"
firstQ = """" + first + """" // (first string, in quotes)
secondQ = """" + second + """"
doesOrNot = [" does not ", " does "]
print firstQ + doesOrNot[first.startsWith(second)] + "start with " + secondQ
print
found = first.findAll(second)
if not found then
print firstQ + " does not contain " + secondQ + " anywhere"
else
for pos in found
print firstQ + " is found at position " + pos + " in " + secondQ
end for
end if
print
print firstQ + doesOrNot[first.endsWith(second)] + "end with " + secondQ