Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 454 Vote(s) - 3.43 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ruby function to remove all white spaces?

#1
What is the Ruby function to remove **all** white spaces? I'm looking for something kind of like PHP's `trim()`?
Reply

#2
Also don't forget:

$ s = " I have white space ".split
=> ["I", "have", "white", "space"]

Reply

#3
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 .
Reply

#4
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"
Reply

#5
Related answer:

" clean up my edges ".strip

returns

"clean up my edges"
Reply

#6
s = "I have white space".delete(' ')

And to emulate PHP's `trim()` function:

s = " I have leading and trailing white space ".strip
Reply

#7
"asd sda sda sd".gsub(' ', '')
=> "asdsdasdasd"
Reply

#8
" Raheem Shaik ".strip

It will removes left & right side spaces.
This code would give us: `"Raheem Shaik"`
Reply

#9
"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)
Reply

#10
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]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through