How Do I Remove The First 4 Characters From A String If It Matches A Pattern In Ruby
Posted
by James
on Stack Overflow
See other posts from Stack Overflow
or by James
Published on 2010-05-22T20:25:54Z
Indexed on
2010/05/22
20:30 UTC
Read the original article
Hit count: 256
I have the following string:
"h3. My Title Goes Here"
I basically want to remove the first 4 characters from the string so that I just get back:
"My Title Goes Here".
The thing is I am iterating over an array of strings and not all have the h3. part in front so I can't just ditch the first 4 characters blindly.
I have checked the docs and the closest think I could find was chomp, but that only works for the end of a string.
Right now I am doing this:
"h3. My Title Goes Here".reverse.chomp(" .3h").reverse
This gives me my desired output, but there has to be a better way right? I mean I don't want to reverse a string twice for no reason.
I am new to programming so I might have missed something obvious, but I didn't see the opposite of chomp anywhere in the docs. Is there another method that will work?
Thanks!
© Stack Overflow or respective owner