This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TNT_Nolan's profile picture

How to check if a string contains something?

Started by TNT_Nolan, 25 April 2016 - 09:35 PM
TNT_Nolan #1
Posted 25 April 2016 - 11:35 PM
Topic explains its all.
Tiin57 #2
Posted 27 April 2016 - 03:37 AM
Well, the best solution is built-in:


local needle = "he"
local haystack = "he dances and runs"
print(string.find(haystack, needle)) -- prints "true"
needle = "this isn't in haystack"
print(haystack:find(needle)) -- prints "false" (if you like OOP-type stuff)

Found here and here.

Edit: Almost forgot to mention that string.find and string.sub act strangely in CC, as noted here
Edited on 27 April 2016 - 01:38 AM