Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby remove first word from string

Tags:

string

ruby

i'm little struggling to find solution of this. How can i remove first word from string like my strings below.

     "i am going to school"

     "he is going to school"

     "she is going to school" 

     "they are going to school"

so string can be any string and don't know about exact length of first word.but just want to remove first word.

The result should be like below

      "am going to school"

     "is going to school"

     "is going to school" 

     "are going to school"

Any help?

Thanks

like image 817
Kashiftufail Avatar asked Mar 25 '13 11:03

Kashiftufail


People also ask

How do you remove the first word of a string in Ruby?

Overview. The chr method removes the first character of a string in Ruby. It removes the one-character string at the beginning of a string and returns this character.

How do you remove something from a string in Ruby?

In Ruby, we can permanently delete characters from a string by using the string. delete method. It returns a new string with the specified characters removed.

How do you get the first character of a string in Ruby?

In Ruby, we can use the built-in chr method to access the first character of a string. Similarly, we can also use the subscript syntax [0] to get the first character of a string. The above syntax extracts the character from the index position 0 .

How do I remove the first element from an array in Ruby?

You can use Array. delete_at(0) method which will delete first element.


1 Answers

"i am going to school".split(' ')[1..-1].join(' ')

=> "am going to school"

You can go both ways in an array in ruby so -1 is the last element.

like image 147
Louis Kottmann Avatar answered Nov 06 '22 04:11

Louis Kottmann