How to get length of a string in Ruby
In this tutorial, we are going to learn about how to get the length of a string in Ruby.
The length of a string means the total number of characters present in a given string.
Using length method
To get the length of a string, we can use the built-in length
method in Ruby.
The
length
method returns the number of characters in a string in integer format.
Here is an example, that gets the length of a name
string:
name = "prince"
length = name.length
puts length
Output:
6
Similarly, we can also use the size
method in ruby.
name = "prince"
length = name.size
puts length