Skip to content
instance method

String#strip

() -> ::String
strip(*selectors) → new_string

Returns a copy of self with leading and trailing whitespace removed; see Whitespace in Strings:

Example
whitespace = "\x00\t\n\v\f\r "
s = whitespace + 'abc' + whitespace
# => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
s.strip # => "abc"

If selectors are given, removes characters of selectors from both ends of self:

Example
s = "---abc+++"
s.strip("-+") # => "abc"
s.strip("+-") # => "abc"

selectors must be valid character selectors (see Character Selectors), and may use any of its valid forms, including negation, ranges, and escapes:

Example
"01234abc56789".strip("0-9") # "abc"
"01234abc56789".strip("0-9", "^4-6") # "4abc56"

Related: see Converting to New String.

Ruby version changes

  1. 4.0
  2. 3.0
See also #strip! #start_with? #squeeze! #sub also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0