Badfitz66
Badfitz66

Reputation: 376

Get part of string after a whitespace

So let's say I have a string called Hi there.

I am currently using

m:match("^(%S+)") 

to get just Hi from the string, now all I need to do is just get "there" from the string but I have no idea how.

Upvotes: 2

Views: 127

Answers (1)

Stratus3D
Stratus3D

Reputation: 4906

Checkout this page: http://lua-users.org/wiki/SplitJoin

There are numerous ways to split words in a string on whitespace.

This one seems like it might be a good fit for your problem:

function justWords(str)
  local t = {} -- create an empty table

  -- create a function to insert a word into the table
  local function helper(word) table.insert(t, word) return "" end 

  -- grab each word in the string and pass it to `helper`
  if not str:gsub("%w+", helper):find"%S" then return t end
end

table = justWords(example)
table[1] -- hi
table[2] -- there
table[3] -- nil

Upvotes: 2

Related Questions