
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
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.
- Related Articles
- Golang Program to convert a string into Uppercase
- Java program to convert a string to lowercase and uppercase.
- Swift program to convert array to a string
- Swift Program to Convert a String to Lowercase
- Convert a String to Uppercase in C
- Swift program to convert string to an array
- Write a C program to convert uppercase to lowercase letters without using string convert function
- Swift Program to convert int type variable to string
- Swift program to convert double type variable to string
- Swift program to convert boolean variable into string
- Swift Program to Convert Set of String to Array of String
- How to convert string to uppercase in TypeScript?
- How to convert a string into uppercase in AngularJS?
- Swift program to convert string type variable into boolean
- Convert string to lowercase or uppercase in Arduino
