4

I import a phone-number from "Contacts" and save in NSString. this string contains white-space and I try to delete them using the method:

numero = numero.stringByReplacingOccurrencesOfString(" ", withString: "")

this method doesn't work.


func sostituisci( stringa: NSString! ) -> NSString     
{         
var numero: NSString = ""         
NSLog(stringa) 
numero = ((stringa as String).stringByReplacingOccurrencesOfString(" ", withString: "") as NSString) 
NSLog(numero)

return numero
}

the output unchanged


log

2014-11-05 17:54:50.734 HappyRicarica[33438:3119446] (327) 124-3503
2014-11-05 17:54:50.737 HappyRicarica[33438:3119446] (327) 124-3503
dario91sr
  • 39
  • 1
  • 6

4 Answers4

4

I suspect that the space character in your string is not really a space. Try adding this after NSLog(string) to see what the unicode scalar values are for the characters in your string:

for uni in (stringa as String).unicodeScalars {
    println("\(uni) = \(uni.value)")
}

The expected output for "(327) 124-3503" is:

( = 40
3 = 51
2 = 50
7 = 55
) = 41
  = 32
1 = 49
2 = 50
4 = 52
- = 45
3 = 51
5 = 53
0 = 48
3 = 51

From your comment, your space has value 160 instead of 32. You could remove that with:

numero = stringa.stringByReplacingOccurrencesOfString(String(Character(UnicodeScalar(160))), withString: "")
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • the log when I use your method: HappyRicarica[33497:3132714] (327) 124-3503 ( = 40 3 = 51 2 = 50 7 = 55 ) = 41   = 160 1 = 49 2 = 50 4 = 52 - = 45 3 = 51 5 = 53 0 = 48 3 = 51 – dario91sr Nov 05 '14 at 23:25
  • I know you aleady accepted an answer. I just wanted you to know why your original code wasn't working. Your space wasn't really a space (ASCII 32) but it had a value of 160 instead. See the update above with how to replace that. The answer you accepted is really nice, so I'd leave that as the accepted answer. – vacawama Nov 06 '14 at 01:33
2

update: Xcode 7.2 • Swift 2.1.1

extension String {
    var numbersOnly: String {
        return componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "1234567890")
            .invertedSet)
            .joinWithSeparator("")
    }
    var numbersExempt: String {
        return componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "1234567890"))
            .joinWithSeparator("")
    }
    var charactersOnly: String {
        return componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").invertedSet).joinWithSeparator("")
    }
    var charactersExempt: String {
        return componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")).joinWithSeparator("")
    }
    func keep(keepIt: String) -> String {
        return componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: keepIt).invertedSet).joinWithSeparator("")
    }
    func exclude(excludeIt: String) -> String {
        return componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: excludeIt)).joinWithSeparator("")
    }
}

let phoneNumber = "+1 (555) 555 - 5555".numbersOnly
print(phoneNumber) // "15555555555"

let excludePlusMinus = "+1 (555) 555-5555".exclude("+-") // "1 (555) 5555555"
let keepWhatever = "+1 (555) 555-5555".keep("()-+") //  "+()-"

you can also use your function to subtract only spaces with some adjustments. Try like this:

func sostituisci(stringa: String) -> String {
     return stringa.stringByReplacingOccurrencesOfString(" ", withString: "")
}

sostituisci("1 234 567 8901")   // "12345678901"

or like an extension:

extension String {
    var sostituisci: String {
        return stringByReplacingOccurrencesOfString(" ", withString: "")
    }
}
let phoneNumber2 = "1 234 567 8901".sostituisci
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
0

did you try :

let str = "XX XX XX XX XX"
    let separated = str.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
    let finalStr = "".join(separated)
ben_
  • 51
  • 4
  • error in line: let finalStr = "".join(separated) -> "'String' is not identical to 'AnyObject' – dario91sr Nov 05 '14 at 16:18
  • 1
    if you replace `let finalStr = "".join(separated)` with `let finalStr = ("" as String).join(separated)` ? – ben_ Nov 05 '14 at 18:10
0

NSString is not String, so you should use:

numero = ((numero as String).stringByReplacingOccurrencesOfString(" ", withString: "") as NSString)
nicael
  • 18,550
  • 13
  • 57
  • 90