Languages: Python C C++ Go Java Ruby Rust Perl R ← Full TOC

← Ruby Index

🔤 String — Built-in string methods — 100+ operations

Ruby strings are mutable UTF-8 objects with over 100 built-in methods. String interpolation with #{} makes formatting natural.

📋 Methods & Functions

NameSignatureDescription
length/sizestr.length → IntegerNumber of characters
upcasestr.upcase → StringReturn uppercase copy
downcasestr.downcase → StringReturn lowercase copy
capitalizestr.capitalize → StringCapitalize first letter
reversestr.reverse → StringReturn reversed string
stripstr.strip → StringRemove leading/trailing whitespace
chompstr.chomp → StringRemove trailing newline
include?str.include?(sub) → boolTrue if str contains sub
start_with?str.start_with?(prefix) → boolTrue if starts with prefix
end_with?str.end_with?(suffix) → boolTrue if ends with suffix
splitstr.split(sep) → ArraySplit on separator
gsubstr.gsub(pattern, replacement) → StringReplace all matches
substr.sub(pattern, replacement) → StringReplace first match
matchstr.match(regex) → MatchDataMatch against regex
match?str.match?(regex) → boolTrue if regex matches (no MatchData)
scanstr.scan(pattern) → ArrayReturn all matches as array
trstr.tr(from, to) → StringTranslate characters
squeezestr.squeeze → StringRemove consecutive duplicate chars
countstr.count(chars) → IntegerCount occurrences of chars
charsstr.chars → ArrayReturn array of characters
bytesstr.bytes → ArrayReturn array of byte values
[]str[n] / str[n,len] / str[regex]Subscript access
<<str << other → strAppend other to str in place
*str * n → StringReturn str repeated n times
to_istr.to_i → IntegerParse as integer
to_fstr.to_f → FloatParse as float
to_symstr.to_sym → SymbolConvert to Symbol
format / %'%s scored %d' % [name, score]printf-style formatting
freezestr.freeze → strMake string immutable
encodingstr.encoding → EncodingReturn string encoding
encodestr.encode('UTF-8') → StringRe-encode string
each_linestr.each_line {|l| block}Iterate over lines
centerstr.center(width, pad) → StringCenter string with padding
ljust/rjuststr.ljust(width) / str.rjust(width)Left/right justify
deletestr.delete(chars) → StringDelete characters in chars
hexstr.hex → IntegerInterpret as hex string
octstr.oct → IntegerInterpret as octal string

💡 Example Program

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

🏠 Index  |  Array →