ruby.onl / regex

String Methods: Your Swiss Army Knife for Text Mangling

2026-03-17

Ruby strings come loaded with methods for every text processing task you can imagine. Splitting, joining, slicing, transliterating, squeezing. If Perl's string functions were scattered across substr, index, uc, lc, and a dozen regex operations, Ruby bundles them all into clean method calls on the string itself.

Part 1: Whitespace Handling

# chomp - remove trailing newline (like Perl's chomp) line = "hello\n" line.chomp # => "hello" (returns new string) line.chomp! # modifies in place # strip - remove leading AND trailing whitespace " hello ".strip # => "hello" " hello ".lstrip # => "hello " (left only) " hello ".rstrip # => " hello" (right only) # chop - remove last character (like Perl's chop) "hello\n".chop # => "hello" "hello".chop # => "hell"

Part 2: Splitting

# split - like Perl's split "a,b,c".split(",") # => ["a", "b", "c"] "a,b,c".split(",", 2) # => ["a", "b,c"] (limit to 2 parts) "hello world".split # => ["hello", "world"] (splits on whitespace) "a::b::c".split(%r~::~) # => ["a", "b", "c"] (regex delimiter) # Perl: split(/,/, $str) # Ruby: str.split(",") or str.split(%r~,~)

Part 3: Joining

# join - opposite of split ["a", "b", "c"].join(",") # => "a,b,c" ["a", "b", "c"].join(" | ") # => "a | b | c" ["a", "b", "c"].join # => "abc" (no separator) # Shortcut with * operator ["a", "b", "c"] * "," # => "a,b,c" # Perl: join(",", @arr) # Ruby: arr.join(",")

Part 4: String Repetition and Concatenation

# Repeat with * operator "ab" * 3 # => "ababab" "-" * 40 # => "----------------------------------------" # Perl: "ab" x 3 # + returns new string greeting = "hello" + " " + "world" # << mutates in place (like Perl's .=) str = "hello" str << " world" # str is now "hello world" # Perl: $str .= " world"

Part 5: Case Methods

"hello".upcase # => "HELLO" "HELLO".downcase # => "hello" "hello world".capitalize # => "Hello world" (first char only) "Hello".swapcase # => "hELLO"

Part 6: Search and Check

# include? - substring check "hello world".include?("world") # => true # start_with? / end_with? - prefix/suffix "hello".start_with?("hel") # => true "file.log".end_with?(".log") # => true # index - find position (like Perl's index) "hello".index("ll") # => 2 "hello".index("x") # => nil # count - count occurrences of characters "hello".count("l") # => 2 "hello world".count("lo") # => 5 (counts l, o individually)

Part 7: Replacement and Deletion

# gsub/sub - see the regex post for full coverage "hello".gsub("l", "r") # => "herro" "hello".sub("l", "r") # => "herlo" (first only) # tr - character transliteration (like Perl's tr///) "hello".tr("el", "ip") # => "hippo" "hello".tr("a-m", "A-M") # => "HELLo" # delete - remove characters "hello".delete("l") # => "heo" # squeeze - collapse repeated characters "aabbcc".squeeze # => "abc" "aabbcc".squeeze("a") # => "abbcc" (only squeeze 'a')

Part 8: Slicing

# By index "hello"[0] # => "h" "hello"[-1] # => "o" "hello"[1..3] # => "ell" "hello"[1, 3] # => "ell" (start, length) # By regex "hello 123 world"[%r~\d+~] # => "123" # Perl: substr($str, 1, 3) # Ruby: str[1, 3]

Part 9: Set Operations on String Arrays

# These work on arrays but are killer for text processing: [1,2,3,4] - [2,4] # => [1,3] (difference) [1,2] & [2,3] # => [2] (intersection) [1,2] | [2,3] # => [1,2,3] (union) [1,2] + [3,4] # => [1,2,3,4] (concat) # Practical example: find lines unique to one file file1_lines = File.readlines("file1.txt").map(&:chomp) file2_lines = File.readlines("file2.txt").map(&:chomp) only_in_file1 = file1_lines - file2_lines

Part 10: Encoding and Conversion

# to_i - string to integer (like Perl's int() or 0+$str) "42".to_i # => 42 "42abc".to_i # => 42 (stops at non-digit) "abc".to_i # => 0 # to_f - string to float "3.14".to_f # => 3.14 # to_s - anything to string 42.to_s # => "42"
Ruby gives you a method for everything, and they're all chainable. That's the real power. line.chomp.strip.downcase.split(",") reads like English, not like hieroglyphics.

Created By: Wildcard Wizard. Copyright 2026