-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.rb
96 lines (63 loc) · 1.75 KB
/
string.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#string concatenation
# str1 = "deepak"
# str2 = 'rawat'
# p str1 + str2
# #string interpolation
# puts "my name is #{str1} singh #{str2}"
# #common methods
# full_name = "deepak singh"
# p full_name.capitalize
# p full_name.empty?
# p ''.empty?
# sentence = "my name is kamal singh rawat"
# p sentence.sub('kamal', 'deepak')
# Substring
# string = "abc123"
# p string[0..-2]
# String Contains Another String
# string = "Today is Saturday"
# p string.include?("Saturday")
# #variable assignment
# first_name = 'deepak'
# new_first_name = first_name
# p new_first_name
# first_name = 'kamal'
# p first_name
# p new_first_name
# #escaping
# p "performing escaping using this arrow \#{first_name}"
#getting error
#p 'deepak 'singh rawat''
# p'hey deepak, \'how u doing?\''
# #get user input
# # puts "whats your name"
# # name = gets.chomp
# # puts "u said ur name is #{name}"
#get user input and print in rev order
# p "enter ur first name"
# first = gets.chomp
# p "enter ur last name"
# second = gets.chomp
# conn = first + ' ' + second
# rev = conn.reverse!
# p "your full name reversed is #{rev}"
#length of string
# p rev.length
# Trim a String & Remove White Space
# extra_space = " test "
# p extra_space.strip
# Taking a string & breaking it down into an array of characters is easy with the split method.
# string = "a b c d"
# p string.split
# Convert an Array to a String
# arr = ['a', 'b', 'c']
# p arr.join
# Iterate Over Characters Of a String in Ruby
# "rubyguides".each_char { |ch| puts ch }
# Replace Text Inside a String Using The Gsub Method
# string = "We have many dogs"
# p string.gsub("dogs", "cats")
#for empty string
# string = "abccc"
# p string.gsub("c", "")
# to remove that extra newline character (\n) is to use the chomp method.