Delete characters
The String
class defines the delete
method for deleting characters. Very useful for filtering out unwanted characters. ⚠️ The method doesn’t delete sub-strings, but each character present in argument. delete!
modifies the instance and returns nil
if there has been no change.
delete
doesn’t let you use a regular expression, but supports 2.5 special characters:
-
to define a range of characters^
for the negation of a set\
to escape one of these 3 characters
The method definition is delete(*selectors)
, the splat operator allows multiple arguments. In this case, the intersection of each set determines the characters to be deleted.
Links to Ruby documentation for delete
and selection rules
.