Ruby function to remove all white spaces? - Printable Version +- 0Day Forums (https://0day.red) +-- Forum: Coding (https://0day.red/Forum-Coding) +--- Forum: Ruby (https://0day.red/Forum-Ruby) +--- Thread: Ruby function to remove all white spaces? (/Thread-Ruby-function-to-remove-all-white-spaces) |
Ruby function to remove all white spaces? - microdistillation983799 - 07-18-2023 What is the Ruby function to remove **all** white spaces? I'm looking for something kind of like PHP's `trim()`? RE: Ruby function to remove all white spaces? - phyllomorphy658381 - 07-18-2023 Also don't forget: $ s = " I have white space ".split => ["I", "have", "white", "space"] RE: Ruby function to remove all white spaces? - claqueurs550975 - 07-18-2023 If you want to remove only leading and trailing whitespace (like PHP's trim) you can use `.strip`, but if you want to remove ***all*** whitespace, you can use `.gsub(/\s+/, "")` instead . RE: Ruby function to remove all white spaces? - mesenchymatal634105 - 07-18-2023 It's a bit late, but anyone else googling this page might be interested in this version - If you want to clean up a chunk of pre-formatted text that a user may have cut & pasted into your app somehow, but preserve the word spacing, try this: content = " a big nasty chunk of something that's been pasted from a webpage or something and looks like this " content.gsub(/\s+/, " ").strip #=> "a big nasty chunk of something that's been pasted from a webpage or something and looks like this" RE: Ruby function to remove all white spaces? - annemarijkerxqhhv - 07-18-2023 Related answer: " clean up my edges ".strip returns "clean up my edges" RE: Ruby function to remove all white spaces? - cucdvkjwgpn - 07-18-2023 s = "I have white space".delete(' ') And to emulate PHP's `trim()` function: s = " I have leading and trailing white space ".strip RE: Ruby function to remove all white spaces? - elaina237 - 07-18-2023 "asd sda sda sd".gsub(' ', '') => "asdsdasdasd" RE: Ruby function to remove all white spaces? - outthanks463823 - 07-18-2023 " Raheem Shaik ".strip It will removes left & right side spaces. This code would give us: `"Raheem Shaik"` RE: Ruby function to remove all white spaces? - heartbird427600 - 07-18-2023 "1232 23 2 23 232 232".delete(' ') => "123223223232232" Delete works faster =) user system total real gsub, s 0.180000 0.010000 0.190000 (0.193014) gsub, s+ 0.200000 0.000000 0.200000 (0.196408) gsub, space 0.220000 0.000000 0.220000 (0.222711) gsub, join 0.200000 0.000000 0.200000 (0.193478) delete 0.040000 0.000000 0.040000 (0.045157) RE: Ruby function to remove all white spaces? - decker160 - 07-18-2023 Ruby's `.strip` method performs the PHP equivalent to `trim()`. To remove all whitespace: " leading trailing ".squeeze(' ').strip => "leading trailing" @Tass made me aware that my original answer removes duplicate letters in succession - YUCK! I've since switched to the squish method which is smarter about such occurrences if using the Rails framework. require 'active_support/all' " leading trailing ".squish => "leading trailing" " good men ".squish => "good men" Cite: [To see links please register here] |