Found 517 Articles for Swift

Swift program to Convert Celsius to Fahrenheit

Ankita Saini
Updated on 16-Jun-2023 11:31:14

545 Views

Fahrenheit is the commonly used temperature-measuring unit. In this scale, the freezing point and boiling point of the water are 32 degrees and 212 degrees. Whereas Celsius is also a temperature-measuring scale. In the Celsius scale, the freezing point and the boiling point of the water are 0 degrees and 100 degrees. So in Swift, we can convert Celsius to Fahrenheit using the following methods − Using Formula Using converted() method Method 1: Using Formula We can easily convert Celsius to Fahrenheit using the mathematical formula. It is the easiest way to convert Celsius to Fahrenheit. Syntax ... Read More

Swift Program to Use Different Types of a Collection

Ankita Saini
Updated on 16-Jun-2023 12:28:26

102 Views

A collection is a group of elements or objects that are gathered together for some specific tasks. Swift supports three types of collections: Array, Set, and Dictionary. They are implemented as generic collections, also they are clear about what type of values they can store which means you can not store the wrong type of values in the collections. Array It is an ordered collection which is used to store similar types of data or elements. It can store duplicate values. It is both mutable and immutable. Syntax var arr :[Type] = [E1, E2, E3] var arr = ... Read More

Swift Program to trim a string from the right side

Ankita Saini
Updated on 16-Jun-2023 11:51:25

90 Views

In Swift, we can adjust the size of the string by trimming some specified number of characters from the right-hand side of the given string. Or we can also trim the extra whitespaces which we do not want on the right-hand side of the given string using the following methods − Method 1: Trim a string from the right side To trim a specified number of characters or a substring from the right side of the string we create a user-defined function which takes an input string and the length of the characters which we wanted to remove ... Read More

Swift Program to trim a string from the left side

Ankita Saini
Updated on 16-Jun-2023 11:56:18

99 Views

In Swift, we can adjust the size of the string by trimming some specified number of characters from the left side of the string. Or we can also trim the extra whitespaces which we do not want on the left side of the original string using the following methods. Method 1: Trim a string from the left side To trim a specified number of characters or a substring from the left side of the string we create a user-defined function which takes an input string and the length of the characters which we wanted to remove from the left side ... Read More

Swift Program to trim a string from both sides

Ankita Saini
Updated on 16-Jun-2023 12:01:35

166 Views

In Swift, we can adjust the size of the string by trimming some specified number of characters from both sides of the string. Or we can also trim the extra whitespaces which we do not want from both sides of the original string using the following methods. Method 1: Trim a string from the both sides To trim a specified number of characters or a substring from both sides of the string we create a user-defined function which takes an input string and the length of the characters which we wanted to remove from both sides of the original ... Read More

Swift Program to Sort a String

Ankita Saini
Updated on 16-Jun-2023 12:06:47

578 Views

In Swift, sorting of a string means arranging the characters of the string in the specified order either in ascending or descending order. For example − Input: String = "color of the bird is pink" Output:" bcdefhiiiklnoooprrst" Here, the resultant string is sorted in ascending order. So to sort a string Swift provides an inbuilt function named sorted() function. This function returns a sorted string in which the characters of the string are stored in either ascending or descending order. This function can be defined in two types with and without parameters. ... Read More

Swift Program to Reverse a String Using Stacks

Ankita Saini
Updated on 16-Jun-2023 12:16:42

615 Views

A stack is a data structure which works on LIFO(last in first out) principle. It is used to store and manage data where the recently added item is the first one to be removed from the stack. Stack support the following operations − Push − It is used to add elements to the stack. So in Swift, we achieve push operation by the append() method. Syntax func push(_ items: T) { item.append(items) } Here, the append() function pushes a new element in the item stack. Pop − It is used to remove ... Read More

Swift Program to Replace the Spaces of a String with a Specific Character

Ankita Saini
Updated on 16-Jun-2023 12:25:07

657 Views

In Swift, we are allowed to replace whitespaces with the specified character like $, *, ! etc. So, to replace the spaces of a string with a specific character swift provide the following methods − Using replacingOccurrences() method Using user-defined method Using components() and joined() methods Using split() and joined() methods Using map() and joined() method Method 1: Using the replacingOccurrences() method The replacingOccurrences() method is used to create a string in which all the occurrences of the target string or character are replaced by the specified string or character. Syntax func replacingOccurrences(of: String, with: String) ... Read More

Swift Program to Replace a Character at a Specific Index

Ankita Saini
Updated on 14-Jun-2023 16:39:54

1K+ Views

To replace a character at a specified index with a new character Swift support the following methods − Using replaceSubrange() method. Using the append() method. Using replacingCharacters(in:with:). Using these methods you can replace any character from the given string with a new character. For Example Input: String = "Pink Bike" Index = 5 Character = "O" Output: "Pink Oike " Here, we replaced the character present on index 5 which is “B” with “O”. Method 1: Using the replaceSubrange() method To replace a character at a specified index in the given ... Read More

Swift Program to Remove leading zeros

Ankita Saini
Updated on 14-Jun-2023 16:35:37

671 Views

While working with strings sometimes we encounter some numeric strings whose leading numbers are zeros, e.g. 000003232, 00321, etc. To remove leading zeros from a numeric string − Using removeFirst() and hasPrefix() methods Using firstIndex() and dropFirst() methods Example Input: String = "000003231" Output: "3231 " Here, the input string contains 5 leading zeros so we remove these zeros from the input string so the resultant string is 3231. Method 1: Using removeFirst() and hasPrefix() methods So to remove leading ... Read More

Advertisements