Ruby Strings

String is combinations of alphabets symbols and characters they are make a word. Character is basic unit of string. In Ruby Programming, string will be defined of within the single and double quotes.

Ruby string literals
test_one = "Ruby Code" #String literals

test_two = 'Python Code' #String literals

#Display String Data
puts test_one
puts test_two

puts "Simple Test" #This is an string literals
Ruby Code
Python Code
Simple Test

String can be used in a two way within our codes. First is in form of constant literals which are used to displaying message or perform to some string operation.

#constant Text Sting Operation
puts "Ruby Text String"[0,4]  #Ruby

puts "Ruby Text String".slice(5,4) #Text

puts "Ruby Text String".include?"String" #true

puts "Ruby Text String".include?"ruby" #false

And second way is assign string literals to variable then performed string operation on to this variable. String variable are immutable, value of string are used by variable name. And character of string are accessible by array index because Internally string is combination of characters arrays.

text_data = "Ruby String" #String literals

puts text_data[0] #R

puts text_data[1] #u
  
puts text_data[2] #b

puts text_data[3] #y

#Access the string portion
#Start with index 5 and get 3 elements
puts text_data[5,3] # Str
R
u
b
y
Str

Interpolation are allowed in string text. That is evaluate the instruction within #{ }. For example

x = 10

puts " x+1 : #{x+1}"
puts " x+2 : #{x+2}"
puts " x+3 : #{x+3}"
puts " x+4 : #{x+4}"
puts " x+5 : #{x+5}"
 x+1 : 11
 x+2 : 12
 x+3 : 13
 x+4 : 14
 x+5 : 15

Unicode characters

puts "\u{950}"   # ॐ
puts "\u{2230}"  # ∰
puts "\u{2131}"  # ℱ
puts "\u{2693}"  # ⚓
puts "\u{2934}"  # ⤴ 
puts "\u{2792}"  # ➒  
puts "\u{2748}"  # ❈  
puts "\u{2730}"  # ✰  
puts "\u{2668}"  # ♨  
puts "\u{2656}"  # ♖ 
ॐ
∰
ℱ
⚓
⤴
➒
❈
✰
♨
♖

String Methods and Operation

Ruby String class have many predefined inbuilt method which are provides flexibility and easierity to perform string operation. In this section are mentioned those string method examples.

Concatenating two string

first = "One"
second = "Two"
third = first+ " "+second

puts third #One Two

Compare Two String

Comparison operator is capable to determine and compare of two string objects. In below example comparison operators are using to compare string objects.

puts "One"=="One" #true

puts "One"=="one" #false

puts "One"<="one" #true

puts "One">="one" #false

puts "One"==="One" #true

String Length

length method are used to find and determine the length of string in ruby. This method are count total character and provide its actual length. whiteSpace and special symbol also count in a character.

puts "Ruby on rails".length #13

puts "A   B   C".length #9

puts "".length #0

puts "nil".length #3
13
9
0
3

Split a String in Ruby

#text which are splitting
text_data = "Hello Ruby Programmer"

split_data = text_data.split(" ") #split by space character

puts split_data
Splitting text element in ruby
Hello
Ruby
Programmer

Strip Method

strip method are remove unwanted tabs and spaces to beginning and at end of the text. And returns new object of string.

text = "   Text        Data   "
newtext = text.strip 
puts text #   Text        Data 
puts newtext #Text        Data
   Text        Data   
Text        Data

Replace Text In String

gsub method are modified text in current object and returning a new String object

text = "check -> check | Ok"
newtext = text.gsub("check","Runing") 

puts text #check -> check | Ok
puts newtext #Runing -> Runing | Ok
check -> check | Ok
Runing -> Runing | Ok

String text iteration

String text iteration are possible each element of string are implemented as character array that value can be retrieved by ruby looping statement. Let see few example.

Example 1

text ="ABCDXYZ"
#simple logic, using of a range operator
(0...text.length).each { |c|
    puts text[c] #display single character
}
A
B
C
D
X
Y
Z

Example 2

data = "ABXYZ"
i = 0
while i<data.length do 

  puts data[i]
  i+=1 #increment index value
end
A
B
X
Y
Z

Reversing Strings Text

text = "GFEDCBA"
puts("Before : #{text}")
text=text.reverse
puts("After : #{text}")
Before : GFEDCBA
After : ABCDEFG

Insert Text

text_data = "Ruby Code"
text_data.insert(0, "Run ")
puts text_data #Run Ruby Code

Chop Method

text = "Running Running"
text = text.chop #remove last character
puts text

Upcase Method

text = "Running CoDe"
text = text.upcase 
puts text #RUNNING CODE

Downcase Method

text = "Running CoDe"
text = text.downcase 
puts text #running code

Capitalize Method

text = "Check TEXT DaTa"
text = text.capitalize 
puts text #Check text data

Swapcase Method

text = "Check TEXT DaTa"
text = text.swapcase 
puts text #cHECK text dAtA

Clear Method

text ="ABCDXYZ"
text.clear
puts text #""


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved