Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a substring from position N to the last char in Ruby?

I'd like to get a substring from a string from position N to the end of the string.

What's the way to do it in Ruby?

like image 224
Only Bolivian Here Avatar asked May 10 '11 03:05

Only Bolivian Here


People also ask

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

Getting the last character To access the last character of a string, we need to pass the negative index -1 to the square brackets [] in Ruby. Note: Negative index gets the data from the end of a string.

What does \n do in Ruby?

In double quoted strings, you can write escape sequences and Ruby will output their translated meaning. A \n becomes a newline. In single quoted strings however, escape sequences are escaped and return their literal definition. A \n remains a \n .

How do you access characters in a string in Ruby?

Accessing Characters Within a String You can also access a single character from the end of the string with a negative index. -1 would let you access the last character of the string, -2 would access the second-to-last, and so on. This can be useful for manipulating or transforming the characters in the string.

How do you get a substring from a string?

To extract a substring that begins at a specified character position and ends before the end of the string, call the Substring(Int32, Int32) method. This method does not modify the value of the current instance. Instead, it returns a new string that begins at the startIndex position in the current string.


1 Answers

Just slice the string like:

string[N..-1] 
like image 184
Sophie Alpert Avatar answered Sep 21 '22 23:09

Sophie Alpert