Ruby String

Let's dive into a tutorial on how to work with strings in Ruby.

Creating Strings

In Ruby, you can create strings by enclosing text in single quotes (') or double quotes (").

string1 = 'Hello, world!'
string2 = "Hello, world!"

The difference between the two is that double quotes allow for escape sequences and interpolation.

Escape Sequences

Escape sequences start with a backslash (\) and signal the Ruby interpreter to treat the following character specially.

puts "Hello\tworld"  # Outputs: Hello   world
puts "Hello\nworld"  # Outputs: Hello
                     #          world

Interpolation

Interpolation allows you to insert any Ruby expression within a string using the syntax #{...}.

name = 'John'
puts "Hello, #{name}"  # Outputs: Hello, John

String Concatenation

You can combine strings using the + operator or the << operator.

greeting = 'Hello, ' + 'world!'  # "Hello, world!"
greeting = 'Hello, ' << 'world!'  # "Hello, world!"

String Methods

Ruby provides a number of useful methods for working with strings.

string = 'Hello, world!'

puts string.length  # Outputs: 13
puts string.upcase  # Outputs: HELLO, WORLD!
puts string.downcase  # Outputs: hello, world!
puts string.capitalize  # Outputs: Hello, world!
puts string.include?('world')  # Outputs: true
puts string[0]  # Outputs: H
puts string.index('world')  # Outputs: 7

Multiline Strings

In Ruby, you can create multiline strings using a "Here Document" or <<-.

multiline_string = <<-EOF
This is a simple
multiline string
EOF

puts multiline_string

Substituting part of a string

You can substitute part of a string in Ruby using sub or gsub methods. sub replaces the first occurrence while gsub replaces all occurrences.

puts "hello".sub('l', 'L')  # Outputs: heLlo
puts "hello".gsub('l', 'L')  # Outputs: heLLo

Splitting Strings

The split method splits a string into an array of substrings based on a delimiter.

puts "Hello, world!".split  # Outputs: ["Hello,", "world!"]
puts "Hello, world!".split(',')  # Outputs: ["Hello", " world!"]

This is a basic introduction to strings in Ruby. Strings have many more methods which allow you to do almost anything you could want to do with a string. As always in Ruby, remember that strings are mutable objects!

  1. String manipulation in Ruby: Strings are mutable in Ruby, allowing various operations.

    str = "Hello, Ruby!"
    
  2. Concatenating strings in Ruby: Combine strings using the + operator or <<.

    greeting = "Hello"
    name = "Ruby"
    full_greeting = greeting + ", " + name + "!"
    
  3. String interpolation in Ruby: Embed expressions in strings using #{}.

    name = "Ruby"
    full_greeting = "Hello, #{name}!"
    
  4. Working with string literals in Ruby: Single-quoted strings are literal; double-quoted strings allow interpolation.

    single_quoted = 'This is a string'
    double_quoted = "This is #{1 + 1}"
    
  5. Substring operations in Ruby strings: Extract substrings using indices or ranges.

    str = "Hello, Ruby!"
    substring = str[7..10]  # "Ruby"
    
  6. Replacing text in Ruby strings: Use sub or gsub for substitution.

    str = "Hello, World!"
    replaced = str.gsub("World", "Ruby")
    
  7. Ruby string methods: Explore various string methods like upcase, downcase, reverse, etc.

    str = "Hello, Ruby!"
    upcased = str.upcase
    
  8. Comparing strings in Ruby: Compare strings using ==, eql?, or casecmp.

    str1 = "Hello"
    str2 = "hello"
    equal = str1.casecmp(str2) == 0
    
  9. String conversion in Ruby: Convert between string and other data types using to_s.

    number = 42
    str = number.to_s
    
  10. Formatting strings in Ruby: Use % for string formatting.

    name = "Ruby"
    formatted = "Hello, %s!" % name
    
  11. Unicode support in Ruby strings: Ruby supports Unicode characters in strings.

    unicode_str = "����ˤ���"
    
  12. Escape characters in Ruby strings: Use escape characters for special characters.

    escaped_str = "This is a line\nThis is a new line"
    
  13. Regular expressions with Ruby strings: Match and manipulate strings using regular expressions.

    str = "Hello, Ruby!"
    matched = str.match(/(\w+), (\w+)!/)