Testing Strings in Strings

Hello everyone. Today I’m here with a little issue. So I’ve been working on another little project for a bit but I’ve been stuck. I need to find a way to make it so if for example,

if myWords == words…“.” Then
isTrue()

So basically if I write “Codea” it will say it Normally, but if I say “Codea.” With a full stop, it will restart or something. How can I do that? How do I check if there’s a full stop there. Is it possible?

Sorry, here’s the code:


function setup()

    words = ""
    dependables = {} -- dont worry about these
parameter.boolean("test")    
    
end

function draw()
    
    background(36, 36, 36, 255)
      
    pushStyle()
    font("Arial-BoldMT")
    fontSize(25)
    fill(0, 161, 255, 255)
    textWrapWidth(WIDTH - 10)
    text(words, WIDTH/2, HEIGHT/2 + 250)

end

function touched(touch)
    
  if touch.state == ENDED then
        showKeyboard()
    end
    
end

function keyboard(key)
    
    words = words..key
    
    if key == BACKSPACE then
       words = string.sub(words, 1, -2)
    end
    if key == RETURN then
        sayWords()
    end
 
    if key == RETURN and words == words.."." then -- heres the problem
        restart() -- test function if it works
    end
end

function sayWords()
    
    speech.language = "EN-GB"
    speech.volume = 1.0
    speech.say(words)
    
end

string.find - Finds for one symbol, etc.
So you need to find a dot -

string.find(str, searchArgument) - Returns nil or a number where searchArgument is called the first time.

Omg I forgot. Wow. Thanks @TokOut . I forgot all about it!

Hold on, how would I get its return value? Whether it’s true or false? string.find doesn’t seem to have a callback value,

@TokOut

@LL_Phoenix456 Instead of searching thru words looking for a ., just compare key for a . and set a flag. If that flag is true, then do a restart after you sayWords().

Sorry was away, @LL_Phoenix456, like this:

str = "Hello World, and here comes the dot."
local d = string.find(str, "%.") -- We need a "%" before the dot, because a dot represents a class
if d then
    -- We have a dot, do something
else
    -- We don't have a dot, do something.
end

Place this in a function. Maybe?

@dave1707, your idea would work as long as he doesn’t have backspaces, when he adds them to remove a letter, the flag will not be false. If he has two dots…

I know how, but I also know how it’s easier

@TokOut If you do a backspace, you check the last character of the string before you remove it. If it’s a . then you set the flag to false.

@dave1707 okay it worked. But just wondering, why do we need to make the dot represent a class?

@LL_Phoenix456 The dot . is just a character like any other one you key in. I’m not sure where the dot is supposed to be a class.

@dave1707 As in the “%” thing you need to put before it. Why?

@LL_Phoenix456 - you need to read up on how string.find works, there are many pages describing it. Please do a little research before asking questions here.

@LL_Phoenix456 I’m not sure of the reason to look for the period when you press return to do a restart. Here’s a little example that lets you key whatever you want and when you press return it says what you keyed. It then clears the words variable and let’s you key something else. You can key as much as you want before pressing return.

displayMode(FULLSCREEN)

function setup()
    words = ""
    font("Arial-BoldMT")
    fontSize(25)
end

function draw()
    background(36, 36, 36, 255)
    fill(0, 161, 255, 255)
    textWrapWidth(WIDTH - 10)
    text(words, WIDTH/2, HEIGHT/2 + 250)
end

function touched(touch)
    if touch.state == ENDED then
        showKeyboard()
    end
end

function keyboard(key)
    words = words..key
    if key == BACKSPACE then
        words = string.sub(words, 1, -2)
    end
    if key == RETURN then
        sayWords()        
    end
end

function sayWords()
    speech.language = "EN-GB"
    speech.volume = 1.0
    speech.say(words)
    words=""
end

@dave1707 - the dot is representing everything