Triming characters from the front of a String


#1

For the life of me I can’t figure out an easy way to remove the first
couple of characters from a String. I looked in my Ruby book, searched
online, and checked the documentation for the String class. I must be
missing something obvious.

Could someone let me know if there is a method that I can use to do
this, or if there is some other trick to it.

Thanks,

The SketchUp Artist


#2

For the life of me I can’t figure out an easy way to remove the first
couple of characters from a String. I looked in my Ruby book, searched
online, and checked the documentation for the String class. I must be
missing something obvious.

To remove all whitespace:
" hello world".lstrip # => “hello world”

To remove the first n characters (destructively):
str = “hello world”
str.slice!(0, 5) # => “hello”
str # => " world"


#3

removed_email_address@domain.invalid wrote:

For the life of me I can’t figure out an easy way to remove the first
couple of characters from a String. I looked in my Ruby book, searched
online, and checked the documentation for the String class. I must be
missing something obvious.

Could someone let me know if there is a method that I can use to do
this, or if there is some other trick to it.

str[x…-1] gives you str without the first x characters.

HTH,
Sebastian


#4

On Jul 27, 9:13 am, Sebastian H. removed_email_address@domain.invalid
wrote:

removed_email_address@domain.invalid wrote:

For the life of me I can’t figure out an easy way to remove the first
couple of characters from a String. I looked in my Ruby book, searched
online, and checked the documentation for the String class. I must be
missing something obvious.
str[x…-1] gives you str without the first x characters.

And
str[0…4]=’’
will replace the first 5 characters in the string with an empty
string.

Likewise
str.sub!( /\A.{5}/m, ‘’ )
will mutate the original string to replace the first 5 characters with
an empty string.


#5

On Jul 27, 8:13 am, Sebastian H. removed_email_address@domain.invalid
wrote:

HTH,
Sebastian

NP: Kreator - Fatal Energy
Ist so, weil ist so
Bleibt so, weil war so

That is just what I needed.

Thank you Sebastian and Florian