Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

String#swapcase

() -> ::String
(:ascii | :lithuanian | :turkic) -> ::String
(:lithuanian, :turkic) -> ::String
(:turkic, :lithuanian) -> ::String
swapcase(*options) → string

Returns a string containing the characters in self, with cases reversed; each uppercase character is downcased; each lowercase character is upcased:

Example
s = 'Hello World!' # => "Hello World!"
s.swapcase         # => "hELLO wORLD!"

The casing may be affected by the given options; see Case Mapping.

Related: String#swapcase!.

Ruby version changes

  1. 3.0
See also #swapcase! #sum #succ! #to_c also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0