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 (1) 

Swap the cases in a string:

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

Scope (1) 

SwapCase automatically threads over a list of strings:

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

Properties and Relations (2) 

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 (2) 

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]=

Publisher

SHuisman

Version History

  • 1.0.0 – 08 August 2019

Related Resources

License Information