Error:

Create new string

# literal
s = 'Test'
# Constructor
s = String.new('Test')

Check if the string is equal to another string

"1" == "1" # => true
"1" == 1 # => false (second object is not string)

Compare two strings

"abc" <=> "efg" # => -1 ('abc' is lexically precede 'efg')

Get character at index

s = "abc"
ch = s[1] # => "b"

Get length of the string

s = "abc"
s.length # => 3

Get substring

s = "abcdefghijk"
s[2..5] # => "cdef"
s[2, 4] # => "cdef"
s[-3]   # => "i"

Split string using delimiter

"a:b:c:d::".split(':') # => ["a", "b", "c", "d"]
"k78a31b77".split(/\d+/) # => ["k", "a", "b"]

Split string to characters

characters = "abcd".split('') # ["a", "b", "c", "d"]

Remove part of the string

s = '0123456789'
s[2..3] = ''
s # => '01456789'

Replace part of the string

s = '0123456789'
s[2..3] = 'test'
s # => '01test456789'

Replace all occurences of the substring

s = 'foo bar foo'
news = s.gsub('foo', 'none') # => "none bar none"
s                            # => "foo bar foo"
s.gsub!('foo', 'none')       # => "none bar none"
s                            # => "none bar none"

Remove all occurences of the substring

s = 'foo bar foo'
news = s.delete('foo') # => " bar "
s                      # => "foo bar foo"
s.delete!('foo')       # => " bar "
s                      # => " bar "

Remove all occurences of character set

s = 'foo bar foo'
news = s.gsub(/[aouie]/, '') # => "f br f"
s                            # => "foo bar foo"
s.gsub!(/[aouie]/, '')       # => "f br f"
s                            # => "f br f"

Convert char to it's ordinal code

'A'.ord # => 65

Convert code to char

65.chr # => "A"

Process string one character at a time

"abcdef".each_char { |c| puts "Char: '#{c}'" }

Reverse string by character

s = 'abcdef'
news = s.reverse # => "fedcba"
s                # => "abcdef"
s.reverse!       # => "fedcba"
s                # => "fedcba"

Reverse string by word

s = "one two three"
news = s.split(/\s+/).reverse.join(' ') # => "three two one"

Expand variable in string

i = 99
s = "Value of i is #{i}" # => "Value of i is 99"

Expand expression in string

i = 99
s = "Value of i + 1 is #{i + 1}" # => "Value of i + 1 is 100"

Uppercase string

s = 'Test'
s.upcase  # => "TEST"
s         # => "Test"
s.upcase! # => "TEST"
s         # => "TEST"

Lowercase string

s = 'tEST'
s.downcase  # => "test"
s           # => "tEST"
s.downcase! # => "test"
s           # => "tEST"

Capitalize all words

s = 'one two three'
news = s.split.map(&:capitalize).join(' ') # => "One Two Three"

Capitalize first word

s = 'one two three'
news = s.capitalize # => "One two three"
s                   # => "one two three"
s.capitalize!       # => "One two three"
s                   # => "One two three"

URL-encode the string

require 'uri'

encoded = URI.encode('just test %') # => "just%20test%20%25"

URL-decode the string

require 'uri'

decoded = URI.decode("just%20test%20%25") # => "just test %"

Trim blanks from the ends of a string

s = '  foo  bar '
news = s.strip # => "foo bar"
s              # => "  foo  bar "
s.strip!       # => "foo bar"
s              # => "foo bar"

# rstrip strips from right side and lstrip strips from left side od the string

Trim newline from the ends of a string

s = "test \n"
s.chomp  # => "test "
s        # => "test \n"
s.chomp! # => "test "
s        # => "test "

Find position of the substring

s = 'foo bar foo'
pos = s.index('bar') # => 4

Check if the string contains another string

s = 'foo bar foo'
s.include?('bar')    # => true
s.include?('foobar') # => false

Check if the string starts with another string

s = 'foo bar foo'
s.start_with?('bar') # => false
s.start_with?('foo') # => true

Check if the string ends with another string

s = 'foo bar foo'
s.end_with?('bar') # => false
s.end_with?('foo') # => true

Convert the string to integer

'15'.to_i     # => 15
'xx15xx'.to_i # => 0

Convert the string to float

'15.5'.to_f     # => 15.5
'xx15xx'.to_f   # => 0

Convert float value to string

f = 1e-1
f.to_s # => "0.1"

Convert integer value to string

i = 999
i.to_s # => "999"

Enumerate all words in the string

s = 'one two three'
s.split(/\s/).each { |w| 
    puts "Word: #{w}" 
}