Clean Strings Efficiently with delete_suffix and delete_prefix

Ruby provides many powerful methods for string manipulation, and among them, delete_suffix and delete_prefix are particularly useful when dealing with structured text. These methods allow you to remove a specified suffix or prefix from a string if it exists, providing a clean and efficient way to handle text processing.

Basic Usage

The delete_suffix and delete_prefix methods help eliminate unwanted leading or trailing parts of a string without requiring complex regular expressions.

delete_suffix

The delete_suffix method removes a given suffix from the string if it ends with that suffix. If the string does not contain the suffix, it remains unchanged.

1
2
3
filename = "report_2025.pdf"
clean_name = filename.delete_suffix(".pdf")
puts clean_name  # Output: "report_2025"

delete_prefix

Similarly, delete_prefix removes a given prefix from a string if it starts with it. If the prefix is not found, the string remains unchanged.

1
2
3
url = "https://example.com"
domain = url.delete_prefix("https://")
puts domain  # Output: "example.com"

Advanced Use Cases

Removing Multiple Prefixes or Suffixes

Sometimes, you may need to remove multiple possible prefixes or suffixes. This can be done using an array and chaining method calls.

1
2
3
4
5
prefixes = ["Mr. ", "Mrs. ", "Dr. "]
name = "Dr. Alice"

clean_name = prefixes.reduce(name) { |str, prefix| str.delete_prefix(prefix) }
puts clean_name  # Output: "Alice"

Alternative for Suffixes:

1
2
3
4
5
suffixes = [".jpg", ".png", ".gif"]
file = "image.png"

clean_file = suffixes.reduce(file) { |str, suffix| str.delete_suffix(suffix) }
puts clean_file  # Output: "image"

Using in File Processing

When working with filenames, delete_suffix is particularly useful for removing extensions before renaming or organizing files.

1
2
3
4
files = ["document.docx", "photo.jpg", "script.rb"]
clean_files = files.map { |file| file.delete_suffix(File.extname(file)) }

puts clean_files  # Output: ["document", "photo", "script"]

Performance Considerations

Unlike regular expressions, delete_suffix and delete_prefix are optimized for direct string comparisons, making them faster and more efficient for simple prefix/suffix removal.

Benchmarking

1
2
3
4
5
6
7
8
9
require 'benchmark'

str = "example_string_suffix"
suffix = "_suffix"

Benchmark.bm do |x|
  x.report("delete_suffix") { 100_000.times { str.delete_suffix(suffix) } }
  x.report("gsub") { 100_000.times { str.gsub(/_suffix$/, '') } }
end
1
2
3
                   user     system      total         real
delete_suffix  0.031387   0.000139   0.031526 (  0.031533)
gsub           0.227606   0.001095   0.228701 (  0.229052)

delete_suffix is generally faster than gsub for removing static suffixes since it avoids regex overhead.

The delete_suffix and delete_prefix methods in Ruby provide a simple yet powerful way to manipulate strings efficiently. They are excellent alternatives to gsub or regex-based solutions when dealing with structured text. Whether cleaning filenames, processing user input, or handling URLs, these methods offer an elegant and performant approach.