![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swift Program to Convert a String to Uppercase
This tutorial will discuss how to write swift program to convert a string to uppercase.
A string is a sequence of characters for example, “RedCar”. Or we can say, string are used to represent textual data. Swift support a String data type which is used to create a String type variable, or we can say to represent strings.
To convert the given string into uppercase Swift provide a in-built function named uppercased(). The uppercased() function is used to convert all the characters(either in lowercase or in uppercase or in both) of the given string into uppercase. This function does not take any parameter.
Below is a demonstration of the same −
Input
Suppose our given input is −
MyStr = “Hello! ToM”
Output
The desired output would be −
Lowercased string = “HELLO! TOM”
Syntax
Following is the syntax −
stringName.uppercased()
Algorithm
Following is the algorithm −
Step 1− Create string with value
Step 2− Convert the string into uppercase using uppercased() function −
var lowerStr = String.uppercased()
Step 3− Display the output
Convert a string to uppercase
Example
The following program shows how to convert a string to uppercase.
import Foundation import Glibc var String1 = "CaR iS In BlUe" var String2 = "ITS RAINING TODAY" var String3 = "i love icecreame" // Convert to uppercase var upperStr1 = String1.uppercased() var upperStr2 = String2.uppercased() var upperStr3 = String3.uppercased() print("Original String:", String1) print("Uppercase String:", upperStr1) print("\nOriginal String:", String2) print("Uppercase String:", upperStr2) print("\nOriginal String:", String3) print("Uppercase String:", upperStr3)
Output
Original String: CaR iS In BlUe Uppercase String: CAR IS IN BLUE Original String: ITS RAINING TODAY Uppercase String: ITS RAINING TODAY Original String: i love icecreame Uppercase String: I LOVE ICECREAME
Here, in the above code, we have three strings named String1, String2, and String3. Now we convert them in uppercase using uppercased() function −
var upperStr1 = String1.uppercased() // Return CAR IS IN BLUE var upperStr2 = String2.uppercased() // Return ITS RAINING TODAY var upperStr3 = String3.uppercased() // Return I LOVE ICECREAME
Compare two strings using uppercased()
Example
The following program shows how to compare two strings using uppercased() function.
import Foundation import Glibc var String1 = "CaR iS REd" var String2 = "car is Pink" print("String 1 =",String1,"\nString 2",String2) // Comparing two strings if (String1.uppercased() == String2.uppercased()){ print("String1 and String2 are equal") } else{ print("String1 and String2 are not equal") }
Output
String 1 = CaR iS REd String 2 car is Pink String1 and String2 are not equal
Here, in the above code, we have two strings named: String1 and String2. Now we check if both the strings are equal or not. So we convert the given strings into uppercase using uppercased() function and then using == we check both are equal or not −
if (String1.uppercased() == String2.uppercased()){ print("String1 and String2 are equal") } else{ print("String1 and String2 are not equal") }
Here both the strings are not equal so we get an output: String1 and String2 are not equal.