Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8501447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:12:56+00:00 2026-06-11T01:12:56+00:00

I have a NSString containing possible unwanted characters that I have to convert to

  • 0

I have a NSString containing possible unwanted characters that I have to convert to the escaped unicode version. More specifically, I need to pass a JSON object (serialized as a string) to a UIWebView via it’s script evaluation method, and some characters tend to raise a JS exception.

So I want to encode all characters mentioned to be unsafe by JSLint which are:

\u0000-\u001f
\u007f-\u009f
\u00ad
\u0600-\u0604
\u070f
\u17b4
\u17b5
\u200c-\u200f
\u2028-\u202f
\u2060-\u206f
\ufeff
\ufff0-\uffff

and replace them with their escaped equivalents.

What’s the best way to do this?

UPDATE: Somehow \u2028 (line separator char) snuck in the JSON

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:12:57+00:00Added an answer on June 11, 2026 at 1:12 am

    Well, it’s not exactly the solution I had in mind, but trimming is also a suitable option

      // Remove unsafe JSON characters
      //
      // http://www.jslint.com/lint.html#unsafe
      //
      if (jsonStr.length > 0) {
        NSMutableCharacterSet *unsafeSet = [NSMutableCharacterSet new];
        void (^addUnsafe)(NSInteger, NSInteger) = ^(NSInteger from, NSInteger to) {
          if (to > from) {
            [unsafeSet addCharactersInRange:NSMakeRange(from, (to - from) + 1)];
          } else {
            [unsafeSet addCharactersInRange:NSMakeRange(from, 1)];
          }          
        };
    
        addUnsafe(0x0000, 0x001f);
        addUnsafe(0x007f, 0x009f);
        addUnsafe(0x00ad, 0);
        addUnsafe(0x0600, 0x0604);
        addUnsafe(0x070f, 0);
        addUnsafe(0x17b4, 0);
        addUnsafe(0x17b5, 0);
        addUnsafe(0x200c, 0x200f);
        addUnsafe(0x2028, 0x202f);
        addUnsafe(0x2060, 0x206f);
        addUnsafe(0xfeff, 0);
        addUnsafe(0xfff0, 0xffff);
    
        jsonStr = [[jsonStr componentsSeparatedByCharactersInSet:unsafeSet] componentsJoinedByString:@""];
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a csv file that is utf-16 containing Tamil characters and displays fine
So, I have an NSArray containing NSString. How can I convert the NSArray to
I've have a .txt file containing an NSString (encrypted using a private key) that
I have an NSString that I'd like to append a { at the beginning
I have a NSString that will be passed as a parameter to my library's
My application gets handed an NSString containing an unsigned int . NSString doesn't have
I have a UITableView containing names that I would like to group (and sort)
I have a view containing a segmented controller and a UIDatePicker that only shows
I have an NSData instance containing <7e0006b5 01012605 06070172 7a>, how can I convert
How can I convert a NSString containing a number of any primitive data type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.