1

I would like to trim a string in Lua but I'm struggling I think because of the special characters in the string.

E.g. str = "RG Ph 0%/15.00bpm"

I would like to remove everything after and including the "/"

so thatstr = "RG Ph 0%"

I have found the following code but I don't think it handles the "%" and "/" properly:

local string_gsub = string.gsub

function string.trimRight( str, char )          
char = char or "%s"          
return ( string_gsub( str, "(" .. char .. "*)$", "" ) )  
end  

Any ideas?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294

1 Answers1

2
local str = "RG Ph 0%/15.00bpm"
str = str:match'[^/]*'
print(str)
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64