Ruby strings are mutable UTF-8 objects with over 100 built-in methods. String interpolation with #{} makes formatting natural.
| Name | Signature | Description |
|---|---|---|
| length/size | str.length → Integer | Number of characters |
| upcase | str.upcase → String | Return uppercase copy |
| downcase | str.downcase → String | Return lowercase copy |
| capitalize | str.capitalize → String | Capitalize first letter |
| reverse | str.reverse → String | Return reversed string |
| strip | str.strip → String | Remove leading/trailing whitespace |
| chomp | str.chomp → String | Remove trailing newline |
| include? | str.include?(sub) → bool | True if str contains sub |
| start_with? | str.start_with?(prefix) → bool | True if starts with prefix |
| end_with? | str.end_with?(suffix) → bool | True if ends with suffix |
| split | str.split(sep) → Array | Split on separator |
| gsub | str.gsub(pattern, replacement) → String | Replace all matches |
| sub | str.sub(pattern, replacement) → String | Replace first match |
| match | str.match(regex) → MatchData | Match against regex |
| match? | str.match?(regex) → bool | True if regex matches (no MatchData) |
| scan | str.scan(pattern) → Array | Return all matches as array |
| tr | str.tr(from, to) → String | Translate characters |
| squeeze | str.squeeze → String | Remove consecutive duplicate chars |
| count | str.count(chars) → Integer | Count occurrences of chars |
| chars | str.chars → Array | Return array of characters |
| bytes | str.bytes → Array | Return array of byte values |
| [] | str[n] / str[n,len] / str[regex] | Subscript access |
| << | str << other → str | Append other to str in place |
| * | str * n → String | Return str repeated n times |
| to_i | str.to_i → Integer | Parse as integer |
| to_f | str.to_f → Float | Parse as float |
| to_sym | str.to_sym → Symbol | Convert to Symbol |
| format / % | '%s scored %d' % [name, score] | printf-style formatting |
| freeze | str.freeze → str | Make string immutable |
| encoding | str.encoding → Encoding | Return string encoding |
| encode | str.encode('UTF-8') → String | Re-encode string |
| each_line | str.each_line {|l| block} | Iterate over lines |
| center | str.center(width, pad) → String | Center string with padding |
| ljust/rjust | str.ljust(width) / str.rjust(width) | Left/right justify |
| delete | str.delete(chars) → String | Delete characters in chars |
| hex | str.hex → Integer | Interpret as hex string |
| oct | str.oct → Integer | Interpret as octal string |
Run with ruby script.rb.
# Ruby String methods
s = " Hello, Ruby World! "
puts s.strip
puts s.upcase.strip
puts s.include?("Ruby")
puts s.split(", ").inspect
puts s.gsub(/[aeiou]/i, "*").strip
puts "ha" * 5
puts "Ruby".chars.sort.join
# String interpolation
name, score = "Alice", 95.5
puts "#{name} scored #{"%.1f" % score}%"
puts "%-10s %05.1f" % [name, score]
# Heredoc
text = <<~HEREDOC
Line 1: Go
Line 2: Ruby
Line 3: Rust
HEREDOC
puts text.lines.map(&:chomp).map.with_index(1){|l,i| "#{i}: #{l}"}.join("\n")
# Regex
email = "user@mywebuniversity.com"
if email.match?(/\A[\w.+-]+@[\w-]+\.[\w.]+\z/)
puts "Valid: #{email}"
puts "Domain: #{email.split('@').last}"
end
# Scan all matches
"The price is $42.99 and $19.50".scan(/\$(\d+\.\d{2})/).each do |price|
puts "Found: $#{price.first}"
end
# ruby string_demo.rb