To convert the first character in a string to uppercase in Ruby, you can use methods such as capitalize()
and string interpolation.
In this example,
str1
with a value of "hello world"
.capitalize()
method to capitalize the first character of str1
.str1 = "hello world"
modified_str1 = str1.capitalize
puts "Modified string is: #{modified_str1}"
Modified string is: Hello world
In this example,
str2
with a value of "ruby"
.capitalize()
method to capitalize the first character of str2
.str2 = "ruby"
modified_str2 = str2.capitalize
puts "Modified string is: #{modified_str2}"
Modified string is: Ruby
In this tutorial, we learned How to convert First Character in String to Uppercase in Ruby language with well detailed examples.