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?

#21

To remove whitespace on both sides:
>Kind of like php's trim()
```Ruby
" Hello ".strip
```
To remove all spaces:
```Ruby
" He llo ".gsub(/ /, "")
```
To remove all whitespace:
```Ruby
" He\tllo ".gsub(/\s/, "")
```
Reply

#22
A lot of suggestions work here, but when I read your question and the specific line saying "removing all the whitespace", what came to my mind was this:

```
" a b c " => "abc"
```

And if that is really what is required, you can do this simple operation

```ruby
wide_string = " a b c "

narrow_string = wide_string.delete(" ")

# you can pass all the different kinds
# of whitespaces that you want to remove

puts narrow_string # => "abc"
```
Reply

#23
Well actually there is a shorter and easier to read way to do this.

Why not just split and join?
````
"s t r i n g".split(" ").join()
Reply

#24
I would use something like this to remove all spaces:

my_string = " Foo \t bar\nbaz quux \n "

my_string.split.join
=> "Foobarbazquux"
Reply

#25
`split.join` will blast all spaces anywhere in the string.

" a b c \nd \t ".split.join
> "abcd"

It's easy to type and remember, so it's nice on the console and for quick hacking. Arguably not welcome in serious code though as it masks the intent.

_(Based on Piotr's comment in [Justicle's answer](

[To see links please register here]

) above.)_
Reply

#26
If you want to remove duplicate spaces and keep single spaces intact, use:

```rb
" Hello World! ".sqeeze(" ").strip
```

This is way faster than `split(" ").join` and other alternatives.
Reply

#27
If I am on Rails I use squish to remove all extra spaces in a string, in Ruby alone I would use strip to remove trailing spaces from beginning an d end of the string and finally if I want the same functionality as squish without having to import ActiveModel, I use this mystring.gsub(/\s{1,}/, ' ').strip
Reply

#28
To remove all whitespace characters, including non-ASCII ones (e.g., non-breaking space), in a string you could try using `gsub` with `:space` as the matcher and `""` (empty string) as the replacement:

```ruby
string = " Some Special Text Values ".gsub(/[[:space:]]+/, "")
puts '[' + string + ']' # "[SomeSpecialTextValues]

string = "   em spaces   ".gsub(/[[:space:]]+/, "")
puts '[' + string.strip + ']' # [emspaces]
```

To remove leading and trailing whitespace characters, you can try:

```ruby
string = "   em spaces   "
.gsub(/^[[:space:]]+/, "")
.gsub(/[[:space:]]+$/, "")
puts '[' + string.strip + ']' # [em spaces]

string = "   em spaces ".strip
puts '[' + string.strip + ']' # [   em spaces]
```

Note how `strip` only removes the trailing whitespaces (i.e., `SPACE U+0020`) while leaving the leading whitespaces (i.e., `EM SPACE U+2003`) as-is. That's because `strip` only removes ASCII whitespace characters.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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