|
Post by Josh on May 4, 2017 20:05:31 GMT
Remove multiple spaces
In this challenge, you must write a program that takes the following string and removes multiple spaces before outputting the result.
Hello, world! I am a string. The string outputted should be
Hello, world! I am a string. Feel free to compete on the size of your program or just post your solution if you'd like! If you're competing on size, please format your solution as the language name followed by the number of characters that your solution contains before writing out the solution in a code block. If you're just submitting a solution for fun, make sure you tell us the language that you wrote it in! Feel free to write an explanation on how your program works.
Good luck!
|
|
|
Post by Bit on May 4, 2017 20:09:48 GMT
Poot. RBX.Lua
local s="Hello, world! I am a string." ripspaces=function(str) return ({str:gsub(" +"," ")})[1] --Replace any space count with just one, and return the first value returned by gsub, which is the string after replacing. end print(ripspaces(s)) --Print za result --Be lazy and enjoy.
|
|
|
Post by Josh on May 4, 2017 20:12:41 GMT
Poot. RBX.Lua local s="Hello, world! I am a string."
ripspaces=function(str)
return ({str:gsub(" +"," ")})[1] --Replace any space count with just one, and return the first value returned by gsub, which is the string after replacing.
end
print(ripspaces(s)) --Print za result
--Be lazy and enjoy.
Nice solution, Has!
|
|
|
Post by cntkillme on May 4, 2017 20:37:06 GMT
Poot. RBX.Lua local s="Hello, world! I am a string." ripspaces=function(str) return ({str:gsub(" +"," ")})[1] --Replace any space count with just one, and return the first value returned by gsub, which is the string after replacing. end print(ripspaces(s)) --Print za result --Be lazy and enjoy. Or just return (str:gsub(" +"," ")). No need to create a table and select the first value.
|
|
|
Post by Bit on May 4, 2017 20:38:11 GMT
Really? I just tested it and it printed 5 along with the result when done without the table. Anyway, just did that one for the lulz.
|
|
|
Post by cntkillme on May 4, 2017 20:39:25 GMT
Notice how I wrapped the gsub call in parenthesis.
|
|
|
Post by Bit on May 4, 2017 20:41:07 GMT
...Right. Welp, the more you know. Here's the table-less way to do it then.
local s="Hello, world! I am a string." ripspaces=function(str) return (str:gsub(" +"," ")) --Replace any space count with just one, and return the first value returned by gsub, which is the string after replacing. end print(ripspaces(s)) --Print za result
--Be lazy and enjoy.
|
|