lquasar
lquasar

Reputation: 55

LUA string, remove all characters after last occurrence of white space

Anyone knows or have idea how to remove all characters after last occurrence of white space in LUA, for example

foo = "This is some string"

to get

bar = "This is some"

Upvotes: 1

Views: 3342

Answers (1)

lhf
lhf

Reputation: 72312

Try

bar = foo:gsub("(.*)%s.*$","%1")

The pattern greedily captures everything until a whitespace is seen and discards the rest of the string. The key point here is greedily, which has the effect of finding the last whitespace.

Upvotes: 2

Related Questions