- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 34476 Articles for Programming
![Ankita Saini](https://www.tutorialspoint.com/assets/profiles/489852/profile/60_2374666-1658225115.jpg)
849 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
![Ankita Saini](https://www.tutorialspoint.com/assets/profiles/489852/profile/60_2374666-1658225115.jpg)
763 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
![Ankita Saini](https://www.tutorialspoint.com/assets/profiles/489852/profile/60_2374666-1658225115.jpg)
843 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
![Ankita Saini](https://www.tutorialspoint.com/assets/profiles/489852/profile/60_2374666-1658225115.jpg)
2K+ 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
![Ankita Saini](https://www.tutorialspoint.com/assets/profiles/489852/profile/60_2374666-1658225115.jpg)
875 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
![Ankita Saini](https://www.tutorialspoint.com/assets/profiles/489852/profile/60_2374666-1658225115.jpg)
5K+ Views
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
![Ankita Saini](https://www.tutorialspoint.com/assets/profiles/489852/profile/60_2374666-1658225115.jpg)
140 Views
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
![Ankita Saini](https://www.tutorialspoint.com/assets/profiles/489852/profile/60_2374666-1658225115.jpg)
581 Views
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
![Ankita Saini](https://www.tutorialspoint.com/assets/profiles/489852/profile/60_2374666-1658225115.jpg)
464 Views
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, 34000 or 0003232. Here we are going to pad a string with 0’s on the left side. Example Input: String = "151" newLength = 6 Output: 000151 Here, we pad the original string with three zeros on the left side. Algorithm Step 1 − Create a function which takes the original string and the length of the new string as arguments. Step 2 − Calculates the total number of ... Read More
![Ankita Saini](https://www.tutorialspoint.com/assets/profiles/489852/profile/60_2374666-1658225115.jpg)
283 Views
An enumeration or enum is a user-defined data type which holds a set of related values. It is defined by using the enum keyword. It is also known as an enum case because it uses case keywords to declare values inside it. In Swift, we are allowed to create an enum by the string value. Syntax enum nameOfEnum: Type { case value1 case value2 case value3 } Let enumVariable = value Here, nameOfEnum represents the name of the enum, value1, value2, etc are values defined in the enum and ... Read More