Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

string.find but with a string pattern?

Asked by 6 years ago

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.

2 answers

Log in to vote
1
Answered by
Mayk728 855 Moderation Voter
6 years ago
Edited 6 years ago

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
0
You should mention the other thing you can do with string patterns. hiimgoodpack 2009 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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

Answer this question