102

I have several strings that look like this:

"((String1))"

They are all different lengths. How could I remove the parentheses from all these strings in a loop?

6

6 Answers 6

193

Do as below using String#tr :

 "((String1))".tr('()', '')
 # => "String1"
6
  • 9
    There's also a destructive version tr! that will modify a string in-place, like my_string.tr!(')(','')
    – bonh
    May 19, 2015 at 20:01
  • 10
    Note that this function is not trimming from the beginning or end, but a full replacement on all text within the string.
    – Jordan
    Dec 21, 2017 at 19:41
  • 3
    Misleading and incorrect. Tr is not a trimming function, it is a replacement function. The two are very different operations.
    – Zane Claes
    May 10, 2018 at 15:41
  • 1
    @ZaneClaes Yes. OP used wrong term to explain what actually the OP wants. The answer has shown how to achieve the output. May 10, 2018 at 18:50
  • AttributeError: 'str' object has no attribute 'tr'
    – Nathan B
    Sep 23, 2022 at 6:47
45

If you just want to remove the first two characters and the last two, then you can use negative indexes on the string:

s = "((String1))"
s = s[2...-2]
p s # => "String1"

If you want to remove all parentheses from the string you can use the delete method on the string class:

s = "((String1))"
s.delete! '()'
p s #  => "String1"
1
  • 10
    This will delete all parentheses, even those in the middle of the string as well, which (seemingly) is not what OP wants. If that is what is wanted this is the most elegant solution. Oct 28, 2013 at 14:47
29

For those coming across this and looking for performance, it looks like #delete and #tr are about the same in speed and 2-4x faster than gsub.

text = "Here is a string with / some forwa/rd slashes"
tr = Benchmark.measure { 10000.times { text.tr('/', '') } }
# tr.total => 0.01
delete = Benchmark.measure { 10000.times { text.delete('/') } }
# delete.total => 0.01
gsub = Benchmark.measure { 10000.times { text.gsub('/', '') } }
# gsub.total => 0.02 - 0.04
1
  • 4
    Four years later... :-) I find if I up your benchmarking by a couple orders of magnitude (1_000_000 runs), that with the same code you use above, I do get delete running slightly faster than tr, with delete at about a 0.92 ratio of tr, and gsub a little less than 1.5x of delete (actually ~ 1.46 of delete, and ~ 1.39 of tr). ymmv of course. This is on Ruby 2.6.3 on a 2018 MBP 13. Thanks for benchmarking these three methods!
    – likethesky
    Aug 18, 2019 at 22:00
21

Using String#gsub with regular expression:

"((String1))".gsub(/^\(+|\)+$/, '')
# => "String1"
"(((((( parentheses )))".gsub(/^\(+|\)+$/, '')
# => " parentheses "

This will remove surrounding parentheses only.

"(((((( This (is) string )))".gsub(/^\(+|\)+$/, '')
# => " This (is) string "
1
  • 2
    +1 for the second example (remove surrounding parentheses only). Oct 21, 2014 at 11:05
1

Here is an even shorter way of achieving this:

1) using Negative character class pattern matching

irb(main)> "((String1))"[/[^()]+/]
=> "String1"

^ - Matches anything NOT in the character class. Inside the charachter class, we have ( and )

Or with global substitution "AKA: gsub" like others have mentioned.

irb(main)> "((String1))".gsub(/[)(]/, '')
=> "String1"
1
  • Your two answers have different results on ((a))b. The first will only return a, the second will return ab
    – Ulysse BN
    Oct 1, 2018 at 12:59
0

Use String#delete:

"((String1))".delete "()"
=> "String1"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.