Tuesday, 11 November 2014

String Methods in Ruby


@s1= "This is Ruby Programming language!!"
@str2= "This is Ruby progRAMMING LANGguage.."
puts @s1.chop       #This is Ruby Programming language!
puts @s1.chomp("..")    #This is Ruby Programming language

puts @str2.count("i") #Counts one or more sets of characters.   output=3
puts @str2.count("P") #output= 1
puts @str2.count("p") #output= 0

puts @str2.downcase
puts @str2.upcase
puts @str2.capitalize
puts @str2.swapcase
puts "++++++++++++=empty? and nil? eg+++++++++++++++++++"
@s3=''
@s4='s'
sn=nil
puts @s3.nil?   #false
puts sn.nil?    #true
puts @s3.empty? #true
puts @s1.empty?   #false
puts "++++++++++++=length eg+++++++++++++++++++"
puts @s3.length
puts @s4.length
puts @str2.length

# Testing for presence of substring
puts "++++++++++++=include eg+++++++++++++++++++"
puts @s4.equal? (@s3)
puts @str2.include?("PROGRAMMING")    #case sensitive
puts @str2.include?("ruby")        #case sensitive
puts @str2.include?("LANG") #case sensitive

puts "++++++++++++=index eg+++++++++++++++++++"
@str2= "This is Ruby progRAMMING LANGguage.."
puts @str2.index("ruby").nil?  #if search string is not found, returns nil..
puts @str2.index("T") #0
puts @str2.index("his")  # 1
puts @str2.index('L',3)       # => 3: index of first l in string at or after position 3
puts "@str2.rindex('g'):  #{@str2.rindex('g')}"        # => 3: index of rightmost l in string- ge..
puts "@str2.rindex('T',2):  #{@str2.rindex('T',2)} "     # => 2: index of rightmost T in string at or before 2
puts "@str2.rindex('i',4):  #{@str2.rindex('i',4)} "     # => 2: index of rightmost i in string at or before 4.
# #checks within first 4 characters..

puts @str2.inspect      #"This is Ruby progRAMMING LANGguage.."
puts "++++++++++++=strip eg+++++++++++++++++++"
s5= "       s5Left"
s6= "s6Right             "
s7= "  s7Both      "

puts "before striping the strings, their lengths are- S5: #{s5.length}, s6: #{s6.length} and s7: #{s7.length}"
puts s5.lstrip.inspect + " and its length after lstrip #{s5.lstrip.length}"
puts s6.rstrip.inspect + " and its length after rstrip #{s6.rstrip.length}"
puts s7.strip.inspect + " and its length after lstrip #{s7.strip.length}"

#inspect method gives the printable verison of a string as: "s7Both" \
puts "++++++++++++=slice eg+++++++++++++++++++"
# slice: Deletes the specified portion from str, and returns the portion deleted. returns nil if not found..
puts @str2.slice("Ruby")
puts @str2.slice("Ruby1").nil?

puts "++++++++++++=split eg+++++++++++++++++++"
s1= "This is a sample STRING..
testing with split method"

puts s1.split("..")
puts s1.split("\n")
puts s1.split(" ")
puts s1.split(" ", 2).inspect   #["This", "is a sample STRING..\ntesting with split method"]  split into 2

puts "++++++++++++=sub(replace in vbs) eg+++++++++++++++++++"
=begin
 method "sub" replaces the search string with other string only once..
 if you want to replace at all occurances, then use gsub method..
syntax:
str.sub(pattern, replacement) [or]
str.sub(pattern) { |match| block }
=end

s1= "This is ruby sample string"
puts s1.sub("is", "IS")     #ThIS is ruby sample string
puts s1.sub("is", "iS")   #ThiS is ruby sample string

puts s1.gsub("is", "IS")  #ThIS IS ruby sample string
puts s1.sub!(/(.)(.)/, '\2\1')   # => hTis is ruby sample string

S1= "hELLO woRLD"
# Checking for prefixes and suffixes: Ruby 1.9 and later
puts S1.start_with? "hE" # => true.  Note singular "start" not "starts"
puts S1.end_with? "bells"  # => false

puts "++++++++++++++++++case comparison+++++++++++++++"
# Case insensitive comparison. (ASCII text only)
# casecmp works like <=> and returns -1 for less, 0 for equal, +1 for greater
puts "world".casecmp("WORLD")  # => 0
puts "a".casecmp("B")          # => -1 (<=> returns 1 in this case)
puts "b".casecmp("a")

puts "+++++++++++++++Reverse examples++++++++++++++++++"
S1= "Helloo!"
puts S1.reverse
puts S1.chomp("o")         # => "hell": remove "o" from end


# Left-justify, right-justify, or center a string in a field n-characters wide.
# There are no mutator versions of these methods. See also printf method.
s = "x"
puts s.ljust(3)          # => "x  "
puts s.rjust(3)          # => "  x"
puts s.center(3)         # => " x "
puts s.center(5, '-')    # => "--x--": padding other than space are allowed
puts s.center(7, '-=')   # => "-=-x-=-": multicharacter padding allowed

No comments:

Post a Comment