39

How do I remove a substring after a certain character in a string using Ruby?

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
JayX
  • 1,754
  • 1
  • 18
  • 27
  • 1
    Well I believe the title is self-explanatory enough. Also, I accepted Jacob's answer about 13 hours ago, just give others some time; normally people don't keep refreshing the same page and waiting for answers. – JayX Dec 20 '10 at 17:26
  • 2
    Editing the title isn't to benefit you. It's to benefit others who are looking for an answer to the same problem. No clarifying your title and question actually hurts you because no one will upvote your question if they think its useless. Just my two cents. – aarona Dec 21 '10 at 00:57
  • 2
    The question doesn't explain how long the substring is, so it is really vague. Apparently you wanted the substring to start immediately after the first instance of a certain character extend all the way to end of the main string. Don't expect us to read minds. Another telepath might ask the same question as you but demand a different answer because she only wants to remove a substring of length 1. – David Grayson Sep 26 '14 at 04:55
  • Possible duplicate (with arguably better answers): [Remove "@" sign and everything after it in Ruby](https://stackoverflow.com/questions/7001625/remove-sign-and-everything-after-it-in-ruby) – Jon Schneider Mar 03 '23 at 19:11

6 Answers6

48
new_str = str.slice(0..(str.index('blah')))

alt text

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
32

I find that "Part1?Part2".split('?')[0] is easier to read.

Ivan -Oats- Storck
  • 4,096
  • 3
  • 30
  • 39
  • 3
    but then you'd have to add back in the ? if you wanted it, so I don't think answers your exact question. – Ivan -Oats- Storck Jan 03 '12 at 22:22
  • I like this answer, because you don't have to check whether "?" is present, it also works with any other string withouth throwing an exception. – 0lli.rocks Sep 22 '17 at 15:21
13

I'm surprised nobody suggested to use 'gsub'

irb> "truncate".gsub(/a.*/, 'a')
=> "trunca"

The bang version of gsub can be used to modify the string.

philant
  • 34,748
  • 11
  • 69
  • 112
5
str = "Hello World"
stopchar = 'W'
str.sub /#{stopchar}.+/, stopchar
#=> "Hello W"
Phrogz
  • 296,393
  • 112
  • 651
  • 745
5

A special case is if you have multiple occurrences of the same character and you want to delete from the last occurrence to the end (not the first one). Following what Jacob suggested, you just have to use rindex instead of index as rindex gets the index of the character in the string but starting from the end. Something like this:

str = '/path/to/some_file'
puts str.slice(0, str.index('/')) # => ""
puts str.slice(0, str.rindex('/')) # => "/path/to"
Redithion
  • 986
  • 1
  • 19
  • 32
0

We can also use partition and rpartitiondepending on whether we want to use the first or last instance of the specified character:

string = "abc-123-xyz"
last_char = "-"

string.partition(last_char)[0..1].join   #=> "abc-"
string.rpartition(last_char)[0..1].join   #=> "abc-123-"