106

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
  • 2
    Remove parentheses, or remove first two and last two characters from a "random" string? (Actually random?) Commented Oct 28, 2013 at 14:42
  • "((String1))"[2...-2] # => "String1" Commented Oct 28, 2013 at 14:44
  • 1
    ruby-doc.org/core-1.9.3/String.html Commented Oct 28, 2013 at 14:46
  • You are asking for a loop to change many string, how are the strings stored? Commented Oct 28, 2013 at 15:01
  • I know how to make a loop through all the strings i just wanted the string manipulation part, sorry i worded that weird Commented Oct 28, 2013 at 15:31

6 Answers 6

199

Do as below using String#tr :

 "((String1))".tr('()', '')
 # => "String1"
Sign up to request clarification or add additional context in comments.

6 Comments

There's also a destructive version tr! that will modify a string in-place, like my_string.tr!(')(','')
Note that this function is not trimming from the beginning or end, but a full replacement on all text within the string.
Misleading and incorrect. Tr is not a trimming function, it is a replacement function. The two are very different operations.
@ZaneClaes Yes. OP used wrong term to explain what actually the OP wants. The answer has shown how to achieve the output.
AttributeError: 'str' object has no attribute 'tr'
|
50

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 Comment

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.
30

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 Comment

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!
23

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 Comment

+1 for the second example (remove surrounding parentheses only).
2

Use String#delete:

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

Comments

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 Comment

Your two answers have different results on ((a))b. The first will only return a, the second will return ab

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.