This question already has answers here:
How to remove spaces from a string in Swift?
(7 answers)
Closed 1 year ago.
I need to remove the spaces in between.
For example: "000 111 2222"
And I'd live to have: "0001112222"
How can I do this?
var str = "000 111 2222"
let newString = str.replacingOccurrences(of: " ", with: "", options: .literal, range: nil)
print(newString)
Related
This question already has answers here:
Detect UIWebView links without click
(2 answers)
Closed 7 days ago.
I have simple attributed string:
let string = NSAttributedString(string: "hello https://www.stackoverflow.com")
When I display that string I would like to see:
hello URL
where URL is clickable link and opens https://www.stackoverflow.com.
The link is not hardcoded, at the time when I replace it, I don't know how much, (if any) links exist there.
EDIT:
Look at the question and compare to the one marked as duplicated. It is NOT DUPLICATED. Please review it wisely and smart.
This should do the trick:
let linkDetector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
let str = "hello at https://www.stackoverflow.com or http://google.com ?"
let attrStr = NSMutableAttributedString(string: str)
let matches = linkDetector.matches(in: attrStr.string, range: NSRange(location: 0, length: attrStr.string.utf16.count))
matches.reversed().forEach { aMatch in //Use `reversed()` to avoid range issues
let linkRange = aMatch.range
let link = (attrStr.string as NSString).substring(with: linkRange) //Or use Range
//Here, you could modify the "link", and compute if needed myURLTitle, like URL(string: link)?.host ?? "myURLTitle"
let replacement = NSAttributedString(string: "myURLTitle", attributes: [.link: link])
attrStr.replaceCharacters(in: linkRange, with: replacement)
}
print(attrStr)
This question already has answers here:
trimmingCharacters not work on iOS 10.3 Xcode8.3
(6 answers)
Closed 3 years ago.
I am removing white space from a string with "trimmingCharacters" but space is not removing .
let number = "123 456"
let replaced = number.trimmingCharacters(in: .whitespaces)
print(username)
print(replaced)
Use replacingOccurrences(of:with:) method on String, i.e.
let number = "123 456"
let replaced = number.replacingOccurrences(of: " ", with: "")
print(replaced) //Output: 123456
Use String replacingOccurrences
let number = "123 456"
let replaced = number.replacingOccurrences(of: " ", with: "")
print(replaced)
or
Use Filter
let removed = number.filter{!$0.isWhitespace}
print(removed)
Output 123456
This question already has answers here:
Find number of spaces in a string in Swift
(3 answers)
Closed 5 years ago.
How do you get the count of the empty space within text?
It would be more helpful to me if explained with an example.
You can either use componentsSeparatedBy or filter function like
let array = string.components(separatedBy:" ")
let spaceCount = array.count - 1
or
let spaceCount = string.filter{$0 == " "}.count
If you want to consider other whitespace characters (not only space) use regular expression:
let string = "How to get count of the empty space in text,Like how we get character count like wise i need empty space count in a text, It would be more helpful if explained with an example."
let regex = try! NSRegularExpression(pattern: "\\s")
let numberOfWhitespaceCharacters = regex.numberOfMatches(in: string, range: NSRange(location: 0, length: string.utf16.count))
Regular expression \\s considers tab, cr, lf and space
Easiest way is to do something like this:
let emptySpacesCount = yourString.characters.filter { $0 == " " }.count
What this does is it takes characters from your string, filter out everything that is not space and then counts number of remaining elements.
You can try this example;
let string = "Whitespace count in a string swift"
let spaceCount = string.characters.filter{$0 == " "}.count
This question already has answers here:
Find out if Character in String is emoji?
(17 answers)
Closed 6 years ago.
func getEmojiTags(text: String) -> [String] {
}
An emoji tag is a combination of two parts, with no spaces between them. If there is a space between them, then it is not an emojiTag.
a character which is an emoji
a string which is not an emoji
For example:
Hello, my name is Jason 😀 🐸 how are you ?-> []
Hello, my name is 😀Jason -> [😀Jason]
Hello, my name is 😀 Jason -> []
I am going to the ⛱beach with some 🙉monkeys 👏 -> [⛱beach, 🙉monkeys]
Try using NSRegularExpression with emoji code ranges.
func emojiTags(str: String) -> [String] {
//A little bit simplified, you may need to define what are your "emoji".
//This is a subset defined in http://stackoverflow.com/a/36258684/6541007 .
let emojiCharacters = "\\U0001F600-\\U0001F64F\\U0001F300-\\U0001F5FF\\U0001F680-\\U0001F6FF\\u2600-\\u26FF"
let pattern = "[\(emojiCharacters)][^\(emojiCharacters)\\s]+"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
let matches = regex.matchesInString(str, options: [], range: NSRange(0..<str.utf16.count))
return matches.map{(str as NSString).substringWithRange($0.range)}
}
let str1 = "Hello, my name is Jason 😀 🐸 how are you ?"
print(emojiTags(str1)) //->[]
let str2 = "Hello, my name is 😀Jason"
print(emojiTags(str2)) //->["😀Jason"]
let str3 = "Hello, my name is 😀 Jason"
print(emojiTags(str3)) //->[]
let str4 = "I am going to the ⛱beach with some 🙉monkeys 👏"
print(emojiTags(str4)) //->["⛱beach", "🙉monkeys"]
This question already has answers here:
NSRange to Range<String.Index>
(16 answers)
Closed 7 years ago.
I ran into a very strange problem today with Swift 2.
I have this simple method to extract a substring based on NSRange:
func substringWithRange(string: String, range: NSRange) -> String {
let startIndex = string.startIndex.advancedBy(range.location)
let endIndex = startIndex.advancedBy(range.length)
let substringRange = Range<String.Index>(start: startIndex, end: endIndex)
return string.substringWithRange(substringRange)
}
With ordinary strings or strings containing unicode characters everything works fine. But one string contains the newline characters "\r\n" and suddenly
let startIndex = string.startIndex.advancedBy(range.location)
is always 1 greater than it should be.
let string = "<html>\r\n var info={};</html>"
let range = NSMakeRange(9, 12)
let substring = substringWithRange(string, range: range)
//Expected: var info={};
//Actual: ar info={};<
//string.startIndex = 0
//range.location = 9
//startIndex after advancedBy = 10
Does anyone know why advancedBy is acting that way and how I can solve this problem?
The reason is that Swift treats \r\n as one character
let cr = "\r"
cr.characters.count // 1
let lf = "\n"
lf.characters.count // 1
let crlf = "\r\n"
crlf.characters.count // 1