- 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
Does Swift have a Trim Method for Strings?
In Swift, you can use the trimmingCharacters(in:) method of the String type to trim parts of a string. It is an instance method that returns a newly created string made by removing the applied character set.
This method accepts a parameter of type CharacterSet to perform leading and trailing characters. You can apply different types of CharacterSet to trim a string.
There are some of the most commonly used CharacterSets, like −
whitespaces
whitespacesAndNewlines
decimalDigits
letters
lowercaseLetters
uppercaseLetters
punctuationCharacters
capitalizedLetters
newlines
You will learn some of them about how to perform different types of trimming of a string.
Whitespaces
Syntax
It is used to remove leading and trailing whitespaces from a string. For example,
trimmingCharacters(in: .whitespaces)
Example
You have to call the trimmingCharacters instance method of the string with the whitespaces CharacterSet.
import Foundation let sentence = " this is a sample string containing leading and trailing spaces " let trimmedString = sentence.trimmingCharacters(in: .whitespaces) print("trimmed string: \(trimmedString)")
Output
trimmed string: this is a sample string containing leading and trailing spaces
WhitespacesAndNewlines
Syntax
It is used to remove leading and trailing spaces along with newline characters from a string.
let sentence = " \n this is a sample string containing newlines, leading and trailing spaces "
Example
This is the string you need to trim leading and trailing spaces and newlines. First, we will trim it by whitespace CharacterSet and then by whitespacesAndNewlines so that you can understand it in a better way.
import Foundation let sentence = " \n this is a sample string containing newlines, leading and trailing spaces " let trimmedString = sentence.trimmingCharacters(in: .whitespaces) print("trimmed string: \(trimmedString)")
Output
trimmed string: this is a sample string containing newlines, leading and trailing spaces
Example
You can see, the leading and trailing whitespaces are trimmed but the newlines are not removed. In order to trim both things at once, you need to use the whitespacesAndNewlines CharacterSet.
import Foundation let sentence = " \n this is a sample string containing newlines, leading and trailing spaces " let trimmedString = sentence.trimmingCharacters(in: .whitespacesAndNewlines) print("trimmed string: \(trimmedString)")
Output
trimmed string: this is a sample string containing newlines, leading and trailing spaces
DecimalDigits
Syntax
It is used to return a character set of decimal values from 0 to 9. For example,
trimmingCharacters(in: .decimalDigits)
Example
You have to call the trimmingCharacters instance method of the string with the decimalDigits CharacterSet.
import Foundation let sentence = "16 is my lucky number same as 24" let trimmedString = sentence.trimmingCharacters(in: .decimalDigits) print("trimmed string: \(trimmedString)")
Output
trimmed string: is my lucky number same as
Example
How does it work with floating values?
import Foundation let sentence = "1.6 is my lucky number same as 24" let trimmedString = sentence.trimmingCharacters(in: .decimalDigits) print("trimmed string: \(trimmedString)")
Output
trimmed string: .6 is my lucky number same as
LowercaseLetters
Syntax
This CharacterSet returns a set of lowercase letters. For example,
trimmingCharacters(in: .lowercaseLetters)
Example
You have to call the trimmingCharacters instance method of the string with the lowercaseLetters CharacterSet.
import Foundation let sentence = "this string is containing lowercase letters" let trimmedString = sentence.trimmingCharacters(in: .lowercaseLetters) print("trimmed string: \(trimmedString)")
Output
trimmed string: string is containing lowercase
As you can see, the leading and trailing lowercase letters have been removed from the string.
PunctuationCharacters
Syntax
This CharacterSet returns a set of punctuation characters. For example,
trimmingCharacters(in: .punctuationCharacters)
Example
You have to call the trimmingCharacters instance method of the string with the punctuationCharacters CharacterSet.
import Foundation let sentence = ";this string is containing punctuation letters." let trimmedString = sentence.trimmingCharacters(in: .punctuationCharacters) print("trimmed string: \(trimmedString)")
Output
trimmed string: this string is containing punctuation letters
Newlinesm
Syntax
This CharacterSet returns a set of newline characters. For example,
trimmingCharacters(in: .newlines)
Example
You have to call the trimmingCharacters instance method of the string with the newlines CharacterSet.
import Foundation let sentence = "\n\nthis \nstring is containing newlines characters\n" let trimmedString = sentence.trimmingCharacters(in: .newlines) print("trimmed string: \(trimmedString)")
Output
trimmed string: this string is containing newlines characters
You can see how to remove leading and trailing newline characters from a string.
Conclusion
The trimmingCharacters() method is the most essential method in the Swift language. Often, you need to perform different types of character trimming.