- 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
Swift Program to trim a string from both sides
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 string as arguments and return a resultant new string.
Example
Input: String = "Ram got first place" trimLength = 2 Output: "m got first pla"
Here, in the resultant string, 2 characters are removed from the start and end of the string because the size of trimLength is 2.
Algorithm
Step 1 − Create a function that takes an input string and trimLength as arguments.
Step 2 − Now this function first checks if the trimLength is greater than or equal to the size of the input string. If yes, then return the original string.
Step 3 − If not, then it will use the index() function to find the new start index from which we will start including characters in the new string.
Step 4 − Again use the index() function to find a new end index to which we will stop including characters in the new string.
Step 5 − Then it creates a new string including all the characters starting from the calculated start index till the end index using the range operator.
Step 6 − Return the new resultant string.
Step 7 − Create a string.
Step 8 − Create a variable and store the length which we want to remove from both sides of the original string.
Step 9 − Call the function and pass string and trimLength as arguments in it.
Step 10 − Display output.
Example
In the following Swift program, we will trim the string from both sides. So we create a function which takes a string and the length which we want to trim from both sides as arguments. Then it will check if the numberOfChar is greater than or equal to the length of the string. If yes, then we do not need to trim the string, so it will return the original string. If not, then the function calculates the start and end index using the index() function. Then use String() initializer along with range parameter([tStartIndex..<tEndIndex]) to extract the resultant substring from the original string. Finally, display the updated string.
import Foundation import Glibc func trimString(str: String, numberOfChar: Int)-> String{ if numberOfChar >= str.count { return str } let tStartIndex = str.index(str.startIndex, offsetBy: numberOfChar) let tEndIndex = str.index(str.endIndex, offsetBy: -numberOfChar) let newStr = String(str[tStartIndex..<tEndIndex]) return newStr } let s = "Ram likes Swift" let result = trimString(str: s, numberOfChar: 4) print("Original String:", s) print("String after trimming:", result)
Output
Original String: Ram likes Swift String after trimming: likes S
Method 2: Trim WhiteSapces from both sides
In Swift, we can also remove whitespaces from both sides of the input string using trimmingCharacters(in:). It returns a new string after removing whitespaces from both ends.
Syntax
func trimmingCharacters(in:.whitespaces)
This function takes one parameter which is a set of characters. To remove white spaces we use whitespaces or whitespacesAndNewlines.
Example
In the following Swift program, we will trim a string from both sides. So create a string with white spaces on both sides, then use the trimmingCharacters() function to remove white spaces from the given string and display the final output.
import Foundation import Glibc let OriginalStr = " Sky Pink " let trimStr = OriginalStr.trimmingCharacters(in: .whitespacesAndNewlines) print(trimStr)
Output
Sky Pink
Conclusion
So this is how we can trim a string from both sides. If you want to remove a specified number of characters from both sides then then you can use method 1. Or if you only want to remove white spaces from both sides, then you can use the predefined trimmingCharacters(in:) function. Both methods work effectively.