
- 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 Lowercase
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.
- Related Articles
- Golang Program to convert a string into lowercase
- 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 Uppercase
- 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 lowercase in TypeScript?
- Is there a MySQL command to convert a string to lowercase?
- Swift program to convert string type variable into boolean
- Convert string to lowercase or uppercase in Arduino
- C++ program to convert kth character to lowercase
