Wolfram Research

Function Repository Resource:

SwapCase

Source Notebook

Convert uppercase letters to lowercase and vice versa

Contributed by: Sander Huisman

ResourceFunction["SwapCase"][string]

converts uppercase letters in string to lowercase and vice versa.

Examples

Basic Examples

Swap the cases in a string:

In[1]:=
ResourceFunction["SwapCase"]["This is a Test!"]
Out[1]=

Scope

SwapCase automatically threads over a list of strings:

In[2]:=
ResourceFunction[
 "SwapCase"][{"This is A string!", "and ANOTHER string!"}]
Out[2]=

Properties and Relations

SwapCase can be programmed by using LowerCaseQ or UpperCaseQ and the functions ToUpperCase and ToLowerCase:

In[3]:=
f = StringJoin[
    Map[If[LowerCaseQ[#], ToUpperCase[#], ToLowerCase[#]] &, Characters[#]]] &;
f["This is A test"]
Out[4]=

The implementation of SwapCase is many times faster, though:

In[5]:=
e = ExampleData[{"Text", "AliceInWonderland"}];
{First@AbsoluteTiming[ResourceFunction["SwapCase"][e];], First@AbsoluteTiming[f[e];]}
Out[6]=

Possible Issues

For nearly all strings, SwapCase is an involutory function:

In[7]:=
str = "This is Just a Test!";
ResourceFunction["SwapCase"]@*ResourceFunction["SwapCase"]@str == str
Out[8]=

There are a few exceptions:

In[9]:=
chars = {"\[Micro]", "ß", "ÿ", "ı", "Ÿ", "ſ", "\[FinalSigma]", "\:03d0", "\[CurlyTheta]", "\[Phi]", "\[CurlyPi]", "\[CurlyKappa]",
    "\[CurlyRho]", "\:03f4", "\[Epsilon]", "ẛ", "ẞ", "ᾀ", "ᾁ", "ᾂ", "ᾃ", "ᾄ", "ᾅ", "ᾆ", "ᾇ", "ᾐ", "ᾑ", "ᾒ", "ᾓ", "ᾔ", "ᾕ", "ᾖ", "ᾗ", "ᾠ", "ᾡ", "ᾢ", "ᾣ", "ᾤ", "ᾥ", "ᾦ", "ᾧ", "ᾳ", "ι", "ῃ", "ῳ", "Ω", "K", "\[DotlessJ]", "\[FormalSigma]", "\[FormalCurlyTheta]", "\[FormalPhi]", "\[FormalCurlyPi]", "\[FormalCurlyKappa]", "\[FormalCurlyRho]", "\[FormalEpsilon]"};
ResourceFunction["SwapCase"]@*ResourceFunction["SwapCase"]@chars
Out[10]=

Some strings will get longer after swapping cases:

In[11]:=
ResourceFunction["SwapCase"]["grüß"]
Out[11]=

Resource History

Related Resources

License Information