Convert the case of a string in apex
In an enterprise application, data stored in the database must follow a consistent format. The Apex case conversion methods help in formatting characters by converting them to either lowercase or uppercase based on the circumstances.
Apex provides the following two built-in methods for case conversion.
toUpperCase()
The toUpperCase() function converts all characters in a string to uppercase.
toLowerCase()
The toLowerCase() function converts all characters in a string to lowercase.
A common use case for this function is converting all characters in a given email to lowercase.
Hello World
HELLO WORLD
Following is an example of converting the case of a string to uppercase in apex:
Loading...
In the above example:
- In the above example,
inputString
, is a string variable with the default value 'Hello World'. - The
toUpperCase()
method is used to convert all characters in inputString to uppercase. - The variable
outputString
stores the result of the string conversion, which is then printed in the output using thesystem.debug()
statement.
Try It Yourself
Loading...