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
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
There was a time when we had to manually write in limitless quantities of phone numbers, names, and other information to transfer contacts from one mobile platform to another. Thanks to cell phones, which do most of the job for us in a short time. Nowadays, iPhones and Androids are the two most dominant smartphones on the market. Some individuals wish to migrate from Android to iPhone devices or from iPhone to Android smartphones or maybe from an older iPhone to their newly bought one, so they need to move their contact list from one phone to another in a ... Read More
Do you sometimes feel that your cherished MacBook Air or Pro can perform better? Do you find that no matter how hard you try, you can't find a solution for your trouble? If you don't want potential buyers to see any of the information stored on your MacBook before you sell it, you must clear the data before selling it. If you are experiencing issues with your MacBook or another Mac, you can follow the methods outlined in our extensive reset guide. You will find step-by-step instructions from the manufacturer that will walk you through resetting your MacBook (or iMac) ... Read More
In Swift, regex is known as a regular expression. It is used to create a pattern which helps in matching or extracting some specific port of the given string. We can create a regex instance with the help of a regular expression in regex literal or string. In this article, we are going to use regex to print the first letter of each word. Example Input: String = "Ram got first place" Output: "Rgfp " Here, the output string contains the first letters of each word present in the given string. In the following examples we are going to ... Read More
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
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
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
A whitespace character is a non-printable character which represents a space in the string. To remove all the whitespaces from a string Swift provides the following methods − Using isWhitespace Property Using component() function Using replacingOccurrences(of:with:) method Using regular expressions Method 1: Using isWhitespace Property isWhitespace property is used to check whether the given character is a whitespace character or not. In this method, we use the isWhitespace property with the filter() method to remove all the whitespaces present in the given string. Syntax var res = str.filter{!$0.isWhitespace} Here, the filter() method is called on the ... Read More
In Swift, pad a string with 0’s is to add 0 in the given string either on the left side or on the right side, for example, 234000 or 00021. Here we are going to pad a string with 0’s on the right side using the following methods − Using User-defined function Using pre-defined function Method 1: Using User-Defined Function To pad a string with 0’s on the right side we create a user-defined function which takes the input string and the total length of the resultant string as arguments and returns the resultant string. Example Input: String ... Read More