String.gsub() remove linebreak?

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

1 Like

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

Hmm, that removed the whole string. :joy: I’m debugging, as it’s most likely my fault. I’ll keep you posted!

Awesome, that did the trick! What about tabs? Is it just the tab inside quotes, or a special code too?

Tab is \t, but if you just want to match all whitespace then you can use str:gsub("%s+", " ")

Thanks! I used \t because the other one also erased all spaces. :smiley: