xJon_as
(Jonas)
April 7, 2019, 2:12pm
#1
Hi there!
I can’t find out how to remove linebreaks using string.gsub.
Example, my string is:
"Hello
World"
I’d like to have “Hello World”.
I’ve tried this:
local str = [string here withouth the brackets]
str = string.gsub(str,"\n"," ")
print(str)
That didn’t work. (I get the string by requesting it from the web, and I’m sure it’s correct.)
Thanks in advance, and kind regards,
Jonas
1waffle1
(Waffle)
April 7, 2019, 2:18pm
#2
Your solution works on your example, but my guess is that \n isn’t the only character you want to replace with spaces. Try str = str:gsub("[\n\r]", " ")
to also find carriage return characters.
1 Like
xJon_as
(Jonas)
April 7, 2019, 2:20pm
#3
Hmm, that removed the whole string. I’m debugging, as it’s most likely my fault. I’ll keep you posted!
xJon_as
(Jonas)
April 7, 2019, 2:22pm
#4
Awesome, that did the trick! What about tabs? Is it just the tab inside quotes, or a special code too?
1waffle1
(Waffle)
April 7, 2019, 2:24pm
#5
Tab is \t
, but if you just want to match all whitespace then you can use str:gsub("%s+", " ")
xJon_as
(Jonas)
April 7, 2019, 2:43pm
#6
Thanks! I used \t because the other one also erased all spaces.