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

separating words in strings?

Asked by 9 years ago

I was wondering how I could separate words that are chatted, so that if I chat "give me all the money" and have it print results I would get -give -me -all -the -money

[EDIT] I figured ^^^ that out. but now I need to know how to access specific words.

game.Players.PlayerAdded:connect(function(Player)
Player.Chatted:connect(function(Message)
    for i in string.gmatch(Message, "%S+") do
    print(i)
end
end)
end)
0
Elaborate 'access specific words'. Goulstem 8144 — 9y
0
Lets say I said some thing with 5 words, and I wanted it to print the 2nd word I said... BSIncorporated 640 — 9y

1 answer

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

I'm answering according to this statement: "Lets say I said some thing with 5 words, and I wanted it to print the 2nd word I said..."

You CAN use some fancy string pattern for this, or if you're lazy like me then just predefine a number variable, then increment the variable for each string match with your generic for loop. Check if the number is the specified number.. then get it!(:

local wordWanted = 2 --The second word
local num = 0
local str = 'Hello, I really like to eat pie'
local word --Make an empty variable to set the word to when you find it

--%w+ is the pattern for any alphanumeric(letter or number)
for i in str:gmatch('%w+') do
    num = num + 1
    if num == wordWanted then
        word = i
    end
end

print(word or 'Did not find the #'..wordWanted..' word') --just for funzies(:

--Would print 'I'
Ad

Answer this question