String#delete
str.delete([other_str]+) -> string Returns:
string · Updated March 13, 2026 · String Methods strings delete character-removal
The delete method removes characters from a string based on the character sets you provide. Unlike gsub which works with patterns, delete operates on literal characters—making it faster for simple character removal tasks.
Syntax
string.delete(character_set [, other_sets]) → new_string
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
character_set | String | Required | The first set of characters to remove |
other_sets | String | Optional | Additional character sets to remove |
Character sets work like a subtraction: delete("aeiou") removes all vowels, while delete("a-z", "0-9") removes all lowercase letters except digits.
Examples
Basic character removal
"hello world".delete("lo")
# => "he wrd"
"password123".delete("0-9")
# => "password"
Multiple character sets
# Remove all vowels AND all consonants
"hello".delete("aeiou", "b-z")
# => "" (everything removed)
# Remove whitespace and punctuation
"Hello, World! ".delete(" ", ",", "!")
# => "HelloWorld"
Common Patterns
Sanitizing user input
def sanitize_username(input)
input.delete("^a-zA-Z0-9_")
end
sanitize_username("user@name#123!")
# => "username123"
Removing unwanted characters
phone = "(555) 123-4567"
phone.delete("()- ")
# => "5551234567"
# Remove all non-digit characters
phone.delete("^0-9")
# => "5551234567"
Cleaning file content
content = "Line1\r\nLine2\r\nLine3"
content.delete("\r\n")
# => "Line1Line2Line3"