string.upper() function in Lua programming


There are certain scenarios in our code that when we are working with the strings, we might want some string to be in uppercase, like consider a very basic and yet famous example of such scenario, the PAN number.

Imagine that you are making a web form in which there’s a field for the PAN number of the user, and since you know that PAN number can’t be in lowercases, we need to take the user input of that field and convert the string into its uppercase.

Converting a string to its uppercase in Lua is done by the string.upper() function.

Syntax

string.upper(s)

In the above syntax, the identifier s denotes the string which we are trying to convert into its uppercase.

Example

Let’s consider a very simple example of the same, where we will convert a string literal into its uppercase.

Consider the example shown below −

 Live Demo

s = string.upper("abc")
print(s)

Output

ABC

An important point to note about the string.upper() function is that it doesn’t modify the original string, it just does the modification to a copy of it and returns that copy. Let’s explore this particular case with the help of an example.

Example

Consider the example shown below −

 Live Demo

s = "abc"
s1 = string.upper("abc")
print(s)
print(s1)

Output

abc
ABC

Also, if a string is already in its uppercase form then nothing will change.

Consider the example shown below −

Example

 Live Demo

s = "A"
string.upper("A")
print(s)

Output

A

Updated on: 19-Jul-2021

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements