How to get rid of a "non-breaking space" inside a Swift string?

Mark Carols

I retrieved some strings from AddressBook.framework's ABRecordCopyValue method by casting the returned value of takeRetainedValue() as a String object.

let unmanagedFirstNameProperty = ABRecordCopyValue(person, kABPersonFirstNameProperty)
if unmanagedFirstNameProperty != nil {
    contactFirstName = unmanagedFirstNameProperty!.takeRetainedValue() as? String
}

let formattedName:String = contactFirstName.replaceOccurrences(of: " ", with: "")
print(formattedName)

I want to get rid of the spaces inside this String object by calling replaceOccurrences(of:with:) method, but that doesn't work (the spaces are still there after I call this method) and I was told by the Apple Framework Reference that this is usually because of the string encoding, containing "non-break spaces" which I've heard a lot about, but I have no idea how to make a "non-break space" into a normal space. Is this even possible? If so, what would be the best approach to make the characters inside a String compatible with the replaceOccurrences(of:with:) method?

Son of a Beach

In your code, where you have contactFirstName.replaceOccurrences(of: " ", with: "") is that space character typed in with the option key held down? If you type a space with the option key held down, you should get a non-breaking space.

Then you can replace it with a normal space, or an empty string if you wish.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python - Beautiful Soup - How to Replace a String With a Non-Breaking Space?

From Dev

How to add non-breaking space to a string in javascript?

From Dev

Python - Beautiful Soup - How to Replace a String With a Non-Breaking Space?

From Dev

remove (non-breaking) space character in string

From Dev

How to put a non-breaking space in an NSString?

From Dev

How to append a non-breaking space in a TextBox?

From Dev

How to modify this regular expression to not match a non-breaking space?

From Dev

How to insert Non-breaking space to NSString or NSMutableAttributedString

From Dev

How to force non-breaking space in reStructuredText, in code environment?

From Dev

How to disable non-breaking space with altgr+space

From Dev

Remove P tag with non-breaking space inside of it via JavaScript or jQuery

From Dev

How to get rid of `forKey` string literals in `UserDefaults` with Swift 3?

From Dev

How can I use a non-breaking space before an inline-block-default element, such as a <button>?

From Dev

How to discard elements with non-breaking space before adding them in an Arraylist

From Dev

How to print `Non-breaking space` to a pdf using apache pdf box?

From Dev

How to get rid of enter in String

From Dev

Non breaking space at the end of the line

From Dev

Zlib with non-breaking space

From Dev

non-breaking space in link

From Dev

Zlib with non-breaking space

From Dev

How to get rid of string concatenation for SQL query

From Dev

Breaking space (Opposite of non-breaking space)

From Dev

Non breaking space between two elements

From Dev

Chrome JavaScript Errors on Non-Breaking Space

From Dev

Append non-breaking space behaviour

From Dev

Non-breaking space not working on Android

From Dev

Akka. How to get rid of business logic inside actors?

From Dev

How to get rid of underline on <span> inside <a> tag with hover?

From Dev

How to get rid of blank spaces around an ImageView inside a CardView

Related Related

  1. 1

    Python - Beautiful Soup - How to Replace a String With a Non-Breaking Space?

  2. 2

    How to add non-breaking space to a string in javascript?

  3. 3

    Python - Beautiful Soup - How to Replace a String With a Non-Breaking Space?

  4. 4

    remove (non-breaking) space character in string

  5. 5

    How to put a non-breaking space in an NSString?

  6. 6

    How to append a non-breaking space in a TextBox?

  7. 7

    How to modify this regular expression to not match a non-breaking space?

  8. 8

    How to insert Non-breaking space to NSString or NSMutableAttributedString

  9. 9

    How to force non-breaking space in reStructuredText, in code environment?

  10. 10

    How to disable non-breaking space with altgr+space

  11. 11

    Remove P tag with non-breaking space inside of it via JavaScript or jQuery

  12. 12

    How to get rid of `forKey` string literals in `UserDefaults` with Swift 3?

  13. 13

    How can I use a non-breaking space before an inline-block-default element, such as a <button>?

  14. 14

    How to discard elements with non-breaking space before adding them in an Arraylist

  15. 15

    How to print `Non-breaking space` to a pdf using apache pdf box?

  16. 16

    How to get rid of enter in String

  17. 17

    Non breaking space at the end of the line

  18. 18

    Zlib with non-breaking space

  19. 19

    non-breaking space in link

  20. 20

    Zlib with non-breaking space

  21. 21

    How to get rid of string concatenation for SQL query

  22. 22

    Breaking space (Opposite of non-breaking space)

  23. 23

    Non breaking space between two elements

  24. 24

    Chrome JavaScript Errors on Non-Breaking Space

  25. 25

    Append non-breaking space behaviour

  26. 26

    Non-breaking space not working on Android

  27. 27

    Akka. How to get rid of business logic inside actors?

  28. 28

    How to get rid of underline on <span> inside <a> tag with hover?

  29. 29

    How to get rid of blank spaces around an ImageView inside a CardView

HotTag

Archive