Found 517 Articles for Swift

Swift Program to demonstrate the string interpolation

Ankita Saini
Updated on 13-Jun-2023 17:13:31

128 Views

In Swift, String interpolation is a great feature to create a new string by embedding the value of a variable, constant, function and literal directly in the given string literal. Or we can say that using string interpolation we can create a string by combining static text and dynamic values. Syntax var myString = “hello! \(x). How are you?” We can perform string interpolation by wrapping a string literal or constant or variable in a pair of parentheses that is prefixed by a backslash(\) for example, \(x). Example 1 In the following Swift program, we will demonstrate how ... Read More

Swift Program to demonstrate the example to write double-quotes in a string

Ankita Saini
Updated on 14-Jun-2023 14:43:16

107 Views

In Swift, a string is a sequence of characters that represent in between double quotes, for example: ”Learn Swift”, “tutorialspoint”, etc. But when you print the string double quotes were removed by the compiler and you will get Learn Swift, tutorialspoint in the output. So if you want to print double quotes in the output, then you have to place the backslash character or escape character(\) before the double quotes you want to print inside the given string. It tells the compiler that the character should be treated as a literal character, it is not part of string syntax. ... Read More

Swift Program to demonstrate the escape sequence characters

Ankita Saini
Updated on 14-Jun-2023 14:38:06

153 Views

In Swift, escape sequence characters are those characters who do not represent themselves when they are used inside a string. Instead, they tell the compiler to perform the specified task. They are non-printable characters. Swift supports the following escape sequence characters in the string − Escape Sequence Characters Description It is a newline character. It tells the compiler to move to the new line. \t It is known as the horizontal tab. It tells the compiler to leave the tab. \r It is known as carriage return. It tells ... Read More

Swift Program to Create random strings

Ankita Saini
Updated on 13-Jun-2023 16:40:57

242 Views

In Swift, a random string is a sequence of characters. It is generated by randomly selecting characters from a set of available characters, for example: “fbbKDvf”, “dvsWFVsvdv”, etc. So to create random strings Swift provides the following methods − Using randomElement() method Using UUID Method 1: Using randomElement() method Swift provides an inbuilt function named as randomElement() method. This function returns a random element from the given sequence or collection. Syntax func randomElement() It returns random elements. If the given collection is empty, then it will return nil. Algorithm Step 1 − Create a ... Read More

Swift Program to check a string contains a specified substring or not

Ankita Saini
Updated on 14-Jun-2023 14:28:31

714 Views

In Swift, a substring is a small sequence of characters present in the large string, for example, “Car color is blue”, then “Car”, “color”, “is”, and “blue” are the substrings of the given string. Swift provides the following methods to check if the string contains the specified substring or not − Using range(of:) method Using contains() method Using Regex Method 1: Using range(of:) method The range(of:) method is used to find the range of the first occurrence of the given substring in the string. So using this method we can check if the given substring is present in ... Read More

Swift Program to Update value of Dictionary using key

Ankita Saini
Updated on 10-May-2023 11:18:05

705 Views

Swift provides a method named as updateValue() method to update the value of the dictionary using a specified key. If the specified key does not exist, then this method will add that key along with its value in the dictionary. Syntax dict.updateValue(nvalue, forKey: nkey) Here, nvalue represents the new value and nkey represents the key to which we want to update or add value. If the given key exists in the dictionary, then its value is replaced by the new value. If the given key does not exist, then it will add the nkey and nvalue in the dictionary. ... Read More

Swift Program to Sort Dictionary by keys

Ankita Saini
Updated on 10-May-2023 11:13:21

530 Views

Swift supports a sorted() method to sort all the elements present in the given dictionary. This method sorts the key-value pairs of the dictionary according to their keys. Syntax func sorted(by:) Here, the value of by parameters are − Greater than(>) − To sort the elements into descending order. Less than(

Swift Program to sort an array in descending order using insertion sort

Ankita Saini
Updated on 10-May-2023 11:10:05

686 Views

In Swift, insertion sort is a sorting technique in which the given array is virtually divided into two parts that are the sorted and the unsorted part. Then the array is searched sequentially, compares two elements from the unsorted part, and moves them into the right place in the sorted part. Using insertion sort, we can easily sort the array elements in ascending or descending order. So in this article, we will learn how to sort an array in descending order using insertion sort. Working of insertion sort Given unsorted array To sort the array in descending order, compare ... Read More

Swift Program to sort an array in ascending order using insertion sort

Ankita Saini
Updated on 10-May-2023 11:05:43

282 Views

In Swift, insertion sort is a sorting technique in which the given array is virtually divided into two parts that are the sorted and the unsorted part. Then the array is searched sequentially, compares two elements from the unsorted part, and moves them into the right place in the sorted part. Using insertion sort, we can easily sort the array elements in ascending or descending order. So in this article, we will learn how to sort an array in ascending order using insertion sort. Working of insertion sort Given unsorted array − To sort the array in ascending order, ... Read More

Swift Program to remove the last specified character from the string

Ankita Saini
Updated on 10-May-2023 13:43:58

251 Views

To remove the last specified character from the string Swift provide a pre-defined remove(at:) function. This function removes a character from the specified position or index. Input String = “Today is cloudy day” Character = “o” Output “Today is cludy day” Where character “o” appears two times in the input string so using the remove(at:) function we removed the last appearance of the “o” character from the string. Hence the output string is “Today is cludy day”. Syntax Str.remove(at:Idx) Where Str is the input string and idx is the valid position/index of the specified character to ... Read More

Previous 1 ... 3 4 5 6 7 ... 52 Next
Advertisements