I have a bunch of addresses as strings that are in the following example format:
8 Smith st, Sydney, New South Wales, Australia
However, I'd like to trim them down to the following format:
8 Smith st, Sydney
How can I acheive this? Thanks.
var myString = " Let's trim the whitespace "
var newString = myString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
//Returns "Let's trim the whitespace"
in your Case, first you have to convert it in Array and then convert it as a string as given in below Example
var myString = "Berlin, Paris, New York, San Francisco"
var myArray = myString.componentsSeparatedByString(",")
//Returns an array with the following values: ["Berlin", " Paris", " New York", " San Francisco"]
in your Case, first you have to convert it in Array and then convert it as a string as given in below Example
var myString = "8 Smith st, Sydney, New South Wales, Australia"
var myArray = myString.componentsSeparatedByString(",")
//Returns an array with the following values: ["8 Smith st", " Sydney", " New South Wales", " Australia"]
if myArray.count > 1
{
println(myArray[0]) //8 Smith st
println(myArray[1]) //Sydney
}
Truncate string/text to Specific Length
If you have entered block of sentence/text and you want to save only specified length out of it text. Add the following extension to Class
extension String {
func trunc(_ length: Int) -> String {
if self.characters.count > length {
return self.substring(to: self.characters.index(self.startIndex, offsetBy: length))
} else {
return self
}
}
}
Use
var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
//str is length 74
print(str)
//O/P: Lorem Ipsum is simply dummy text of the printing and typesetting industry.
str = str.trunc(40)
print(str)
//O/P: Lorem Ipsum is simply dummy text of the
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.