0

i'm getting my contacts number locally from my mobile. There are some number in which there are white spaces between numbers. I'm trying to remove the white spaces from the number but it isn't working,this is how i'm removing the white spaces,

let number = contact.phoneNumbers.first?.value.stringValue
let formattedString = number?.replacingOccurrences(of: " ", with: "")
print(formattedString)

But when i print this is what i got in the console,

+92 324 4544783
The white sapces are still coming how can i remove that?
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
Hamza
  • 125
  • 1
  • 10
  • https://stackoverflow.com/a/39067610/8417137 – Alex Kolovatov Aug 29 '18 at 11:53
  • number?.trimmingCharacters(in: .whitespaces) – sazid008 Aug 29 '18 at 11:57
  • I have also tried this but still shows spaces in some numbers. @sazid008 – Hamza Aug 29 '18 at 11:59
  • clean build your project. because your code number?.replacingOccurrences(of: " ", with: "") seems correct also. – sazid008 Aug 29 '18 at 12:21
  • i have done that not working bro. @sazid008 – Hamza Aug 29 '18 at 12:22
  • please don't make it duplicate its an issue that i'm facing . I know that it is ask many times but not working in my condition. @vadian – Hamza Aug 29 '18 at 12:23
  • paste your (number) value here. get that from debugger. – sazid008 Aug 29 '18 at 12:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179025/discussion-between-sazid008-and-hamza). – sazid008 Aug 29 '18 at 12:28
  • @Hamza It **is** a duplicate. Did you read all answers? There are answers to trim the string (remove whitespace from beginning and end) and removing whitespace within the string. – vadian Aug 29 '18 at 12:31
  • e.g. when you pick like phone # from Addressbook, there is some special char looks like space " ", but its not space. Try This contact.phoneNumbers[0].phoneNumber.replacingOccurrences(of: " ", with: "").replacingOccurrences(of: " ", with: "") – Hardik Darji Aug 29 '18 at 12:36
  • PS: In the special case of phone numbers the most reliable solution is `let formattedString = number.replacingOccurrences(of: "[^0-9+]", with: "", options: .regularExpression)`. It cuts everything out except digits and `+`. – vadian Aug 29 '18 at 12:43

2 Answers2

2

Here you go: source

For trimming white spaces from both ends, you can use:

let number = contact.phoneNumbers.first?.value.stringValue
let formattedString = number.trimmingCharacters(in: .whitespacesAndNewlines)
print(formattedString)

For removing whitespaces that might be inside the string, use:

let x = "+92 300 7681277"
let result = x.replacingOccurrences(of: " ", with: "")

You should get:

result = +923007681277

EDIT: I updated my answer.

Mihai Erős
  • 1,129
  • 1
  • 11
  • 18
1
let number = contact.phoneNumbers.first?.value.stringValue
let number_without_space = number.components(separatedBy: .whitespaces).joined()
print(number_without_space) //use this variable wherever you want to use

joined() is a function it will join your string after removing spaces like this

let str = "String    Name"
str.components(separatedBy: .whitespaces).joined()

extension to remove spaces

extension String
{ 
         func removeSpaces() -> String { 
              return components(separatedBy: .whitespaces).joined() 
       }
 } 
Mahesh Dangar
  • 806
  • 5
  • 11