4

How to create NSString of zero width space.

In Objective-C, I used to create like this:

NSString *emptyText = @"\u200B";

How to create it in Swift? I have tried following, but it is not working.

let emptyString = "\u200B"
Khawar
  • 9,151
  • 9
  • 46
  • 67
  • `let emptyString = "\u200B"` gives the compiler error "expected hexadecimal code in braces after unicode escape", and searching for that error message would lead you quickly to http://stackoverflow.com/questions/25225074/expected-hexadecimal-code-in-braces-after-unicode-escape :) – Martin R Dec 03 '14 at 12:54

1 Answers1

14

Use this syntax in Swift

let emptyString = "\u{200B}"

Of course you can also use unicode characters directly in your Swift code.

Nikos M.
  • 13,685
  • 4
  • 47
  • 61
  • How would the unicode character look directly in Swift? – Shai Mishali Jun 03 '15 at 11:34
  • @ShaiMishali Unicode characters (like emojis) can be used in swift directly, without using the unicode code for the character (for example instead of \u{200B} you can assign directly an emoji as a value to a string). – Nikos M. Jun 03 '15 at 14:43
  • Using the Zero Width Space character directly in your code would probably not be a good idea since it is invisible. – Suragch Jul 14 '15 at 23:31
  • @Suragch yes thery are, how to detect them if you add them by a mistake? – Markus Nov 23 '16 at 09:04
  • @Markus, I imagine you could use Xcode's search function. If that doesn't work, you could write a short method to search for them yourself. – Suragch Nov 23 '16 at 13:12