Reputation: 25338
How can I remove everything in a string before a specific word (or including the first space and back)?
I have a string like this:
12345 Delivered to: Joe Schmoe
I only want Delivered to: Joe Schmoe
So, basically anything from the first space and back I don't want.
I'm running Ruby 1.9.3.
Upvotes: 0
Views: 89
Reputation: 26640
If Delivered isn't always the 2nd word, you can use this way:
s_line = "12345 Delivered to: Joe Schmoe"
puts s_line[/\s.*/].strip #=> "Delivered to: Joe Schmoe"
Upvotes: 0
Reputation: 4676
Quite a few different ways are possible. Here are a couple:
s = '12345 Delivered to: Joe Schmoe'
s.split(' ')[1..-1].join(' ') # split on spaces, take the last parts, join on space
# or
s[s.index(' ')+1..-1] # Find the index of the first space and just take the rest
# or
/.*?\s(.*)/.match(s)[1] # Use a reg ex to pick out the bits after the first space
Upvotes: 0
Reputation: 8293
Use a regex to select just the part of the string you want.
"12345 Delivered to: Joe Schmoe"[/Delive.*/]
# => "Delivered to: Joe Schmoe"
Upvotes: 3