Substring in Ruby
The substring is a part of string/variable. We can create substrings from a ruby string variable. This substring can be one character length or multiple characters length.
The below mention is the input string.
str1 = 'Ruby on Rails Developer'
- Print first character of a Ruby string
str1[0]
#Output: R
- Print first
n
characters of a Ruby string
str1[0..3]
#Output: Ruby
- Print last character of a Ruby string
str1[-1]
#Output: r
- Print
n<sup>th</sup>
character of a Ruby string
str1[5]
#Output: o