Ruby String Tutorial

1. Introduction

Strings are among the most widely used objects in programming. In Ruby, the String class provides a multitude of methods that help in the manipulation and inspection of string objects. In this tutorial, we will explore the various methods and operations associated with strings in Ruby.

String in Ruby is a sequence of characters enclosed between single (') or double (") quotes. They can contain letters, numbers, symbols, and more.

Common Methods:

1. length: Returns the length of a string.

2. upcase: Converts the string to uppercase.

3. downcase: Converts the string to lowercase.

4. capitalize: Capitalizes the first character.

5. gsub: Global substitution within a string.

6. include?: Checks if a substring exists within a string.

7. index: Finds the position of a substring.

8. split: Divides a string based on a delimiter.

9. strip: Removes leading and trailing whitespaces.

10. reverse: Reverses the string.

11. concat or +: Concatenates strings.

12. *: Repeats the string.

13. chomp: Removes the newline character from the end of a string.

2. Program Steps

1. Initialize a sample string.

2. Apply and demonstrate the common methods on the initialized string.

3. Code Program

# Initialize a sample string
sample_string = "Hello, Ruby World!\n"
puts "Original String: #{sample_string}"
# 1. Get the length
length = sample_string.length
# 2. Convert to uppercase
uppercase_string = sample_string.upcase
# 3. Convert to lowercase
lowercase_string = sample_string.downcase
# 4. Capitalize the string
capitalized_string = sample_string.capitalize
# 5. Replace 'Ruby' with 'Programming'
replaced_string = sample_string.gsub('Ruby', 'Programming')
# 6. Check if string includes 'World'
includes_world = sample_string.include?('World')
# 7. Find the position of 'Ruby'
index_ruby = sample_string.index('Ruby')
# 8. Split the string by comma
splitted_string = sample_string.split(',')
# 9. Remove whitespaces
stripped_string = "   Too much space.   ".strip
# 10. Reverse the string
reversed_string = sample_string.reverse
# 11. Concatenate strings
concatenated_string = sample_string.concat(" It's a wonderful place!")
# 12. Repeat the string twice
repeated_string = sample_string * 2
# 13. Remove newline character
chomped_string = sample_string.chomp
puts "\nTransformed Strings:"
puts "Uppercase: #{uppercase_string}"
puts "Lowercase: #{lowercase_string}"
puts "Capitalized: #{capitalized_string}"
puts "Replaced: #{replaced_string}"
puts "Includes 'World': #{includes_world}"
puts "Index of 'Ruby': #{index_ruby}"
puts "Splitted: #{splitted_string.inspect}"
puts "Stripped: #{stripped_string.inspect}"
puts "Reversed: #{reversed_string.inspect}"
puts "Concatenated: #{concatenated_string}"
puts "Repeated: #{repeated_string}"
puts "Chomped: #{chomped_string}"

Output:

Original String: Hello, Ruby World!
Transformed Strings:
Uppercase: HELLO, RUBY WORLD!
Lowercase: hello, ruby world!
Capitalized: Hello, ruby world!
Replaced: Hello, Programming World!
Includes 'World': true
Index of 'Ruby': 7
Splitted: ["Hello", " Ruby World!\n"]
Stripped: "Too much space."
Reversed: "!dlroW ybuR ,olleH"
Concatenated: Hello, Ruby World!
 It's a wonderful place!
Repeated: Hello, Ruby World!
Hello, Ruby World!
Chomped: Hello, Ruby World!

Explanation:

1. The length method gives the number of characters in the string.

2. upcase and downcase change the case of the characters.

3. capitalize only capitalizes the first character.

4. gsub searches for a substring and replaces it.

5. include? checks if a substring is present.

6. index returns the position of the first occurrence of the substring.

7. split divides the string based on a specified delimiter.

8. strip removes any leading or trailing whitespaces.

9. reverse creates a new string with characters in opposite order.

10. concat or + appends one string to another.

11. * repeats the string a specified number of times.

12. chomp is particularly useful when reading input or files, to remove the newline character.


Comments