instance method
String#swapcase
() -> ::String
(:ascii | :lithuanian | :turkic) -> ::String
(:lithuanian, :turkic) -> ::String
(:turkic, :lithuanian) -> ::String
swapcase(mapping = :ascii) → new_string
string.c
View on GitHub ↗
static VALUE
rb_str_swapcase(int argc, VALUE *argv, VALUE str)
{
rb_encoding *enc;
OnigCaseFoldType flags = ONIGENC_CASE_UPCASE | ONIGENC_CASE_DOWNCASE;
VALUE ret;
flags = check_case_options(argc, argv, flags);
enc = str_true_enc(str);
if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return str_duplicate(rb_cString, str);
if (flags&ONIGENC_CASE_ASCII_ONLY) {
ret = rb_str_new(0, RSTRING_LEN(str));
rb_str_ascii_casemap(str, ret, &flags, enc);
}
else {
ret = rb_str_casemap(str, &flags, enc);
}
return ret;
}
Returns a string containing the characters in self, with cases reversed:
-
Each uppercase character is downcased.
-
Each lowercase character is upcased.
Examples:
Example
'Hello'.swapcase # => "hELLO" 'Straße'.swapcase # => "sTRASSE" 'Привет'.swapcase # => "пРИВЕТ" 'RubyGems.org'.swapcase # => "rUBYgEMS.ORG"
The sizes of self and the upcased result may differ:
Example
s = 'Straße' s.size # => 6 s.swapcase # => "sTRASSE" s.swapcase.size # => 7
Some characters (and some character sets) do not have upcase and downcase versions; see Case Mapping:
Example
s = '1, 2, 3, ...' s.swapcase == s # => true s = 'こんにちは' s.swapcase == s # => true
The casing is affected by the given mapping, which may be :ascii, :fold, or :turkic; see Case Mappings.
Related: see Converting to New String.
Ruby version changes
-
3.0
-
The following methods now return or yield
Stringinstances instead of subclass instances when called on subclass instances: [Bug #10845]
-