Swift - Remove white spaces from string doesn't work - ios

i'm trying to remove white spaces and some characters from a string, please check my code below
// giving phoneString = +39 333 3333333
var phoneString = ABMultiValueCopyValueAtIndex(phone, indexPhone).takeRetainedValue() as! String
// Remove spaces from string
phoneString = phoneString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
// Remove +39 if exist
if phoneString.rangeOfString("+39") != nil{
phoneString = phoneString.stringByReplacingOccurrencesOfString("\0", withString: "+39", options: NSStringCompareOptions.LiteralSearch, range: nil)
}
print(phoneString) // output +39 333 3333333
it seems like all the changes has no effect over my string, why this happen?
EDIT #V S
EDIT 2:
I tried to convert my string in utf 8, check the result:
43 51 57 194 160 51 51 51 194 160 51 51 51 51 51 51 51
where:
43 = +
51 = 3
57 = 9
160 = space
194 = wtf?!? is this?

what do you try to do is
// your input string
let str = "+39 333 3333333"
let arr = str.characters.split(" ").map(String.init) // ["+39", "333", "3333333"]
// remove country code and reconstruct the rest as one string without whitespaces
let str2 = arr.dropFirst().joinWithSeparator("") // "3333333333"
to filter out country code, only if exists (as Eendje asks)
let str = "+39 123 456789"
let arr = str.characters.split(" ").map(String.init)
let str3 = arr.filter { !$0.hasPrefix("+") }.joinWithSeparator("") // "123456789"
UPDATE, based on your update.
160 represents no-breakable space. just modify next line in my code
let arr = str.characters.split{" \u{00A0}".characters.contains($0)}.map(String.init)
there is " \u{00A0}".characters.contains($0) expression where you can extend the string to as much whitespace characters, as you need. 160 is \u{00A0} see details here.
Update for Swift 4
String.characters is deprecated. So the correct answer would now be
// your input string
let str = "+39 333 3333333"
let arr = str.components(separatedBy: .whitespaces) // ["+39", "333", "3333333"]
// remove country code and reconstruct the rest as one string without whitespaces
let str2 = arr.dropFirst().joined() // "3333333333"

Firstly, stringByTrimmingCharactersInSet only trims the string - i.e. removes leading & trailing spaces - you need to use stringByReplacingOccurrencesOfString replacing " " with "".
Secondly, your parameters on stringByReplacingOccurrencesOfString for the country code are the wrong way round.
Thirdly, "\0" is not what you want- that's ASCII null, not zero.

Swift 3 / Swift 4
let withoutSpaces = phoneNumber.replacingOccurrences(of: "\\s", with: "", options: .regularExpression)

Swift 5
//MARK:- 3 ways to resolve it
var tempphone = "0345 55500 93"
//MARK:- No 1
tempphone = tempphone.replacingOccurrences(of: " ", with: "")
//MARK:- No 2
tempphone = tempphone.replacingOccurrences(of: "\\s", with: "", options: .regularExpression)
//MARK:- No 3
tempphone = tempphone.trimmingCharacters(in: .whitespaces)

phoneString = phoneString.stringByReplacingOccurrencesOfString("+39", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
phoneString = phoneString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())

Try this. This has worked for me:
if phoneString.rangeOfString("+39") != nil{
freshString = phoneString.stringByReplacingOccurrencesOfString("\0", withString: "+39", options: NSStringCompareOptions.LiteralSearch, range: nil)
}
var strings = freshString.componentsSeparatedByString(" ") as NSArray
var finalString = strings.componentsJoinedByString("")
//outputs +393333333333

You can use this replace the whitespace
phoneNumber.replacingOccurrences(of: "\u{00A0}", with: "")

let trimmedPhoneString = String(phoneString).stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
To Remove +39 if exist, you can use stringByReplacingOccurrencesOfString instead

var phoneString = "+39 333 3333333"
phoneString = phoneString.stringByReplacingOccurrencesOfString(" ", withString:"")
if phoneString.rangeOfString("+39") != nil
{
phoneString = phoneString.stringByReplacingOccurrencesOfString("+39", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
}
print(phoneString) // output 3333333333

Related

How to remove first word from a sentence in swift

I know how to remove first character from a word in swift like this:
var data = "CITY Singapore"
data.removeFirst()
print(data)//ITY Singapore
what i want is to remove the first word and space so the result is "Singapore".
How can i remove the first word and leading space in swift?
You can try
let data = "CITY Singapore"
let res = data.components(separatedBy: " ").dropFirst().joined(separator: " ")
print(res)
Or
let res = data[data.range(of: " ")!.upperBound...] // may crash for no " " inside the string
Or you can go with this too
let strData = "CITY Singapore"
if let data = strData.components(separatedBy: " ").dropFirst().first {
// do with data
}
else {
// fallback
}
This is a Regular Expression solution, the benefit is to modify the string in place.
The pattern searches from the beginning of the string to the first space character
var data = "CITY Singapore"
if let range = data.range(of: "^\\S+\\s", options: .regularExpression) {
data.removeSubrange(range)
}
You can use String's enumerateSubstrings in range method (Foundation) using byWords option and remove the first enclosing range. You need also to stop enumeration after removing the range at the first occurrence:
var string = "CITY Singapore"
string.enumerateSubstrings(in: string.startIndex..., options: .byWords) { (_, _, enclosingRange, stop) in
string.removeSubrange(enclosingRange)
stop = true
}
string // "Singapore"

How to convert any textfield input to float in Swift

I want to store float to CoreData and I want to convert every of the following inputs to 90.5:
90.5
90,5
90.5
90, 5
That means: Remove whitespace and convert , to .
Is this code best practice?
let str = " 90, 5 "
let converted = str.trimmingCharacters(in: .whitespacesAndNewlines)
let converted = strWithoutWithespace.replacingOccurrences(of: ",", with: ".")
No, it's not because it doesn't remove the space within the string.
The regex pattern "\\s+" removes all occurrences of one or more whitespace characters.
let str = " 90, 5 "
let strWithoutWhitespace = str.replacingOccurrences(of: "\\s+", with: "", options: .regularExpression)
let converted = strWithoutWhitespace.replacingOccurrences(of: ",", with: ".")

How to remove '\u{ef}' character from String Swift

let's say I have a string
var a = "#bb #cccc #ddddd\u{ef}"
and i am setting it to textview like this
let text = a.trimmingCharacters(in: .whitespacesAndNewlines)
let textRemoved = text?.replacingOccurrences(of: "\u{ef}", with: "", options: NSString.CompareOptions.literal, range:nil)
textView.text = textRemove
I am trying to remove the \u{ef} character here. But in textRemoved it is not happening. Please help me how to do it.
I am using Xcode 10. Looks like below Xcode version than 10 is working
fine. is it a bug of Xcode 10?
This is a late answer but I struggled to replace "\u{ef}" in string as well. During debugging when hovered over string it showed presence of \u{ef} but when print in description it only showed space.
let str = "\u{ef} Some Title"
print(str) //" Some Title"
I tried replacingOccurrences(of: "\u{ef}", with: "", options: NSString.CompareOptions.literal, range: nil).trimmingCharacters(in: .whitespaces) but it failed as well.
So I used below snippet and it worked like wonder.
let modifiedStr = str.replacingOccurrences(of: "\u{fffc}", with: "", options: NSString.CompareOptions.literal, range: nil).trimmingCharacters(in: .whitespaces)
print(modifiedStr) //"Some Title"
Hope this helps someone!!
i also faced same issue for "\u{e2}". i have searched a lot but unable to find any answer. then i have tried below code , which works for me.
var newString = ""
for char in strMainString.unicodeScalars{
if char.isASCII{
newString += String(char)
}
}
Hope that will also work for you too.
In Xcode 10 Playground, string replaces for \u{00EF} is working.
var a = "#bb #cccc #ddddd\u{ef}"
a = a.replacingOccurrences(of: "\u{00EF}", with: "")
I hope that will work for you.
I tried the following and it worked like a charm:
replacingOccurrences(of: "�", with: " ", options: NSString.CompareOptions.literal, range: nil)
e.g. 1
let text = "\u{ef}\u{ef}\u{ef}\u{ef}😇哦哦哦"
let text1 = text.replacingOccurrences(of: "\u{fffc}", with: "", options: String.CompareOptions.literal, range: nil)
let text2 = text.replacingOccurrences(of: "\u{ef}", with: "", options: String.CompareOptions.literal, range: nil).trimmingCharacters(in: .whitespaces)
runnable
<img src="https://i.stack.imgur.com/styVo.png"/>
e.g. 2
let strBefore = textDocumentProxy.documentContextBeforeInput
let strAfter = textDocumentProxy.documentContextAfterInput
var textInput = strBefore + strAfter
let textInput2 = textInput.replacingOccurrences(of: "\u{ef}", with: "", options: String.CompareOptions.literal, range: nil)
let textInput1 = textInput.replacingOccurrences(of: "\u{fffc}", with: "", options: String.CompareOptions.literal, range: nil).trimmingCharacters(in: .whitespaces)
runnable
<img src="https://i.stack.imgur.com/xGHtW.png"/>
Similar to question but with \u{e2} symbol (fix is the same):
\u{e2} is not a character rather subset of UTF8 plane which starts with 0xE2 byte.
So look here, E2 are general punctuation symbols.
There many symbols actually which started with \u{e2} but not limited to it and full char can be represented f.e. with e2 80 a8 bytes (line separator).
That explains why shown in Xcode \u{e2} can't be replaced with replacingOccurrences... function. In order to filter out correct symbol you have to know what exact symbol it is, f.e. by using the snippet below:
"\u{2028}&😲".forEach { (char) in
print(Data(char.utf8).map { String(format: "%02x", $0) }.joined(separator: " "))
}
it prints to console:
e2 80 a8
26
f0 9f 98 b2
which are byte representation for each symbol.
Next step is to filter your string, go here and search in 3d column your bytes and unicode code point value is what you need (first column) and write it in swift code like "\u{2028}\u{206A}..." (depending on your sorting).
The final function may look like:
func removingE2Symbols() -> String {
let specialChars = "\u{202A}\u{202C}"
return filter { !specialChars.contains($0) }
}
Try this
extension String {
var asciiString: String {
return String(self.unicodeScalars.filter{ $0.isASCII })
}
}
It,s working Please check again:
let a = "#bb #cccc #ddddd\u{ef}"
let text = a.trimmingCharacters(in: .whitespacesAndNewlines)
let textRemoved = text.replacingOccurrences(of: "\u{ef}", with: "", options: NSString.CompareOptions.literal, range:nil)
print(textRemoved)

extract only words from sentence which includees numbers

i am using the TesseractOCR to read a receipt and i have managed to extract the text from the receipt line by line e.g
2 melon £3.00
1 lime £1.50
5 chicken wings £10.00
But now, for each line, i would like to extract the item name(melons, lime, chicken wings), then the integer and then the float all sepearately line by line. I have googled a lot and have written this in ruby using regex but cant figure out how to do it in swift. I have figured out the float and integer part just not the words only part.
a link to an answer already would be great or an answer. thanks for any help in advance.
If you have solved this using regex in Ruby, the solution in Swift is similar. First let's define some helper functions since NSRegularExpression still deals in NSRange units:
extension String {
var fullRange: NSRange {
return NSMakeRange(0, self.characters.count)
}
subscript(range: NSRange) -> String {
let startIndex = self.index(self.startIndex, offsetBy: range.location)
let endIndex = self.index(startIndex, offsetBy: range.length)
return self[startIndex..<endIndex]
}
}
And the code:
let text =
"2 melon £3.00\n" +
"1 lime £1.50\n" +
"5 chicken wings £10.00"
let regex = try! NSRegularExpression(pattern: "(\\d+)\\s+(.+?)\\s+£([\\d\\.]+)$", options: [.anchorsMatchLines])
regex.enumerateMatches(in: text, options: [], range: text.fullRange) { result, flag, stop in
if let result = result {
let r1 = result.rangeAt(1)
let r2 = result.rangeAt(2)
let r3 = result.rangeAt(3)
print("quantity = \(text[r1]), item = \(text[r2]), price = \(text[r3])")
}
}
use componentSeparatedByString
let a = "5 Chicken Wing"
let b = a.componentSeparatedByString(" ") //meaning space
let b0 = b[0] //5
let b1 = b[1] //Chicken
let b2 = b[2] //Wing

Regex replace html string in Swift

I am trying to use Regex in Swift to replace an HTML string by a string. Basically anytime there is a set of numbers such as '1, 2 and 3 " preceded by the word 'Appendices' or a single number such 1 preceeded by the world 'Appendix' , I would like to create hyperlink tags for it.
For example I have a string:
See Appendices 1 , 9 and 27. You should also see the Appendices 28, 45 and 37. Also see Appendix 19. See also chapter 19 and Verses 38 and 45
And I would like to replace it with:
See Appendices <a href="Appendix://1"/>1</a> , <a href="Appendix://9"/>9</a> and <a href="Appendix://27"/>27</a> . You should also see the Appendices <a href="Appendix://28"/>28</a> , <a href="Appendix://45"/>45</a> and <a href="Appendix://37"/>37</a> . Also see <a href="Appendix://19"/>Appendix 19</a> . See also chapter 19 and Verses 38 and 45
I ended up writing a method that does this:
func findAndReplaceAppendixDeeplinks(theText:String)->String{
var text = theText
var innerRangeIncrement:Int = 0
do {
let regex = try? NSRegularExpression(pattern: "(Appendix|Appendices|App.) (\\d+)((, |and|&)?( )?(\\d+)?)+", options: NSRegularExpressionOptions.CaseInsensitive)
let range = NSMakeRange(0, text.characters.count)
let matches = regex!.matchesInString(text, options: NSMatchingOptions.WithoutAnchoringBounds, range: range)
innerRangeIncrement = 0
for match in matches {
let theMatch:String = (text as NSString).substringWithRange(match.range)
print("the new match is \(theMatch)")
do {
let regex1 = try? NSRegularExpression(pattern: "(\\d+)", options: NSRegularExpressionOptions.CaseInsensitive)
let innerMatches = regex1!.matchesInString(theText, options: NSMatchingOptions.WithoutAnchoringBounds, range: match.range)
for innerMatch in innerMatches{
let innerString:String = (theText as NSString).substringWithRange(innerMatch.range)
print("innerString is \(innerString)")
let replacementString = "\(innerString)"
printIfDebug("replacementString is \(replacementString)")
let innerRange = NSRange(location: innerMatch.range.location + innerRangeIncrement , length: innerMatch.range.length)
print("now looking for character position \(innerMatch.range.location + innerRangeIncrement)")
text = regex1!.stringByReplacingMatchesInString(text, options: NSMatchingOptions.WithoutAnchoringBounds, range: innerRange, withTemplate: replacementString)
innerRangeIncrement = innerRangeIncrement + replacementString.length - innerString.length
printIfDebug("inner increment value is \(innerRangeIncrement)")
printIfDebug(text)
}
printIfDebug("outer increment value is \(innerRangeIncrement)")
}
}
}
return text
}

Resources