Ruby - Apply String method on string value

Introduction

The following code shows how to use some string methods.

Demo

s = "hello world" 
puts s.length            #=> 11 
puts s.reverse!          #=> Hello world 
puts s.reverse           #=> dlrow olleH  
puts s.upcase            #=> HELLO WORLD 
puts s.capitalize        #=> Hello world 
puts s.swapcase          #=> hELLO WORLD 
puts s.downcase          #=> hello world 
puts s.insert(7,"NOT ")  #=> hello wNOT orld 
puts s.squeeze           #=> helo wNOT orld 
puts s.split             #=> ["helo", "wNOT", "orld"]
#   ww w  .j a  v a 2 s  .  c o m

Result

Related Topic