I'm trying to create a search box where when you search something, it will only return the ones who contain that keyword. I think string.find would be the best solution but I don't know how to make it so that no matter if the keyword contains caps and lower cases, it will still find the matching string.
✓ ANSWER ACCEPTED
string.find but with a string pattern?
2 answers
✓ Accepted answer
To check if the text matches regardless of caps, use string.lower.
Using string.lower;
local text = "BLoXY"
print(text:lower()) --returns "bloxy"
Finding the word "bloxy" in a sentence using string.find and string.lower;
local newtext = "BloXY MaN"
if newtext:lower():find("bloxy") then
print('bloxy was found in the text!')
end
Instead of creating a pattern that checks for both cases, just change the case of the input. Here is an example of how I would implement a search function:
...
print(inputTextBox.Text) --> "Hello, WORLD!"
local searchInput = string.lower(inputTextBox.Text)
print(searchInput) --> "hello, world!"
--[[
Returns a table with search results formatted like ...
{
[TextBox2] = {10, 15},
[TextBox5] = {7, 12}
}
... where TextBox2 and TextBox5 are TextBoxes with text that matched
the search input. The arrays {10, 15} and {7, 12} are 'tuple-like'
arrays marking the begin and end character indices of the result such
that {foundBegin, foundEnd}.
]]
local function search(tableOfTextBoxes, searchInput)
local results = {}
for _, textBox in pairs(tableOfTextBoxes) do
local searchSubject = string.lower(textBox.Text)
-- If we find the input in the subject, store where the matching
-- word's letters begin and end.
local foundBegin, foundEnd =
string.find(searchSubject, searchInput)
if foundBegin then
results[textBox] = {foundBegin, foundEnd}
end
end
return results
end
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
You should mention the other thing you can do with string patterns.