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:
  • 207 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ruby string strip defined characters

#1
In Python, we can use the [`.strip()`][1] method of a string to remove leading or trailing occurrences of chosen characters:

<pre><code>>>> <b>print " (Removes (only) leading & trailing brackets & ws ) ".strip(" ()")</b>
'Removes (only) leading & trailing brackets & ws'</code></pre>

How do we do this in Ruby? Ruby's [`strip`][2] method takes no arguments and strips only whitespace.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#2
Ruby now includes full support for stripping with

lstrip: Strips only leading part of string
rstrip: Strips only ending part of string
strip: Strips both leading and ending of string
Reply

#3
You can use:
str.chomp('.')
that works for trailing characters and you can reverse the string for leading characters:
str.reverse.chomp('.').reverse

To do both at once you can:
str.chomp('.').reverse.chomp('.').reverse

Note: chomp removes only one occurrence by default

Reply

#4
There is no such method in ruby, but you can easily define it like:

def my_strip(string, chars)
chars = Regexp.escape(chars)
string.gsub(/\A[#{chars}]+|[#{chars}]+\z/, "")
end

my_strip " [la[]la] ", " []"
#=> "la[]la"
Reply

#5
Try the `String#delete` method: (avaiable in 1.9.3, not sure about other versions)

Ex:

1.9.3-p484 :003 > "hehhhy".delete("h")
=> "ey"
Reply

#6
There is no such method in ruby, but you can easily define it like:

class String
alias strip_ws strip
def strip chr=nil
return self.strip_ws if chr.nil?
self.gsub /^[#{Regexp.escape(chr)}]*|[#{Regexp.escape(chr)}]*$/, ''
end
end

Which will satisfy the requested requirements:

> "[ [] foo [] boo [][]] ".strip(" []")
=> "foo [] boo"

While still doing what you'd expect in less extreme circumstances.

> ' _bar_ '.strip.strip('_')
=> "bar"


nJoy!
Reply

#7
"[[ ] foo [] boo ][ ]".gsub(/\A[ \[\]]+|[ \[\]]+\Z/,'')
=> "foo [] boo"

Can also be shortenend to

"[[ ] foo [] boo ][ ]".gsub(/\A[][ ]+|[][ ]+\Z/,'')
=> "foo [] boo"
Reply

#8
Try the [gsub][1] method:

irb(main):001:0> "[foo ]".gsub(/\As+[/,'')
=> "foo ]"

irb(main):001:0> "foo ]".gsub(/s+]\Z/,'')
=> "foo"

etc.


[1]:

[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