
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert String to Lowercase in Swift
This tutorial will discuss how to write swift program to convert a string to lowercase.
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 lowercase Swift provide a in-built function named lowercased(). The lowercased() function is used to convert all the characters(either in lowercase or in uppercase or in both) of the given string into lowercase. 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.lowercased()
Algorithm
Following is the algorithm ?
Step 1 ? Create string with value
Step 2 ? Convert the string into lowercase using lowercased() function ?
var lowerStr = String.lowercased()
Step 3 ? Display the output
Convert a string to lowercase
Example
The following program shows how to convert a string to lowercase.
import Foundation import Glibc var String1 = "CaR iS In BlUe" var String2 = "ITS RAINING TODAY" var String3 = "i love icecreame" // Convert to lowercase var lowerStr1 = String1.lowercased() var lowerStr2 = String2.lowercased() var lowerStr3 = String3.lowercased() print("Original String:", String1) print("Lowercase String:", lowerStr1) print("\nOriginal String:", String2) print("Lowercase String:", lowerStr2) print("\nOriginal String:", String3) print("Lowercase String:", lowerStr3)
Output
Original String: CaR iS In BlUe Lowercase String: car is in blue Original String: ITS RAINING TODAY Lowercase String: its raining today Original String: i love icecreame Lowercase String: i love icecreame
Here, in the above code, we have three strings named String1, String2, and String3. Now we convert them in lowercase using lowercased() function ?
var lowerStr1 = String1.lowercased() // Return car is in blue var lowerStr2 = String2.lowercased() // Return its raining today var lowerStr3 = String3.lowercased() // Return i love icecreame
Compare two strings using lowercased()
Example
The following program shows how to compare two strings using lowercased() function.
import Foundation import Glibc var String1 = "CaR iS REd" var String2 = "car is RED" // Comparing two strings if (String1.lowercased() == String2.lowercased()){ print("String1 and String2 are equal") } else{ print("String1 and String2 are not equal") }
Output
String1 and String2 are 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 lower case using lowercased() function and then using == we check both are equal or not ?
if (String1.lowercased() == String2.lowercased()){ print("String1 and String2 are equal") } else{ print("String1 and String2 are not equal") }
Here both the strings are equal so we get an output: String1 and String2 are equal.