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 8692711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:22:18+00:00 2026-06-13T00:22:18+00:00

I need to parse the following string in Objective-C for iOS app NSString *htmlString

  • 0

I need to parse the following string in Objective-C for iOS app

NSString *htmlString = @”12, 22, ‘stringA’,”, ‘stringB, stringC’, 2,’stringD'”;

I would like to an array like this

{
    @12,
    @22,
    @"stringA",
    @"emptySlotInfo",
    @"stringB, stringC",
    @2,
    @"stringD"
}

The headache is @”strinb, stringC” because

[htmlString componentsSeparatedByString:@","];

does not work for the case and the @”‘” as separator does not work either.

How can I get necessary components?

  • 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-13T00:22:20+00:00Added an answer on June 13, 2026 at 12:22 am

    You could use NSScanner.

    If it scans a ', it knows a string is starting and ignore ,till it reads the next '. if no opening ' was read, sperate by ,.

    This cocoawithlove article might be helpful.


    I made a quick prototype. Most likely there is much to optimize, as I am also not a expert for NSScanner

    NSString *htmlString = @"12, 22, 'stringA','', 'stringB, stringC', 2,'stringD'";
    NSScanner *scanner = [NSScanner scannerWithString:htmlString];
    
    NSString *apostrophe = @"'";    // scanner needs to detect this
    NSString *comma = @",";         // scanner needs to detect this
    NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:[NSString stringWithFormat:@"%@%@", apostrophe, comma]];
    BOOL apostropheOpen = NO;       // is the scan location inside a single quoted substring?
    NSInteger lastCommaIndex = -1;  // track last found comma's index
    NSMutableArray *array = [NSMutableArray array];
    
    while (![scanner isAtEnd]) {
        [scanner scanUpToCharactersFromSet:charSet intoString:NULL];
        NSString *charAtlocation = [htmlString substringWithRange:NSMakeRange([scanner scanLocation], 1)];
        if ([charAtlocation isEqualToString:apostrophe]){
            apostropheOpen = !apostropheOpen;                
        } else if ([charAtlocation isEqualToString:comma]){
            if (!apostropheOpen) {
                [array addObject: [scanner.string substringWithRange:NSMakeRange(lastCommaIndex+1, [scanner scanLocation]- lastCommaIndex-1)]];
                lastCommaIndex = [scanner scanLocation];
            }
        }
        [scanner setScanLocation:[scanner scanLocation]+1];
    } ;
    
    // scanner only dealt with the string until the last comma, probably one more value to handle
    if (lastCommaIndex < [scanner scanLocation]){
        [array addObject: [scanner.string substringWithRange:NSMakeRange(lastCommaIndex+1, [scanner scanLocation]- lastCommaIndex-1)]];
    }
    
    // array contains seperated strings, but with blanks and apostrophes
    // we will deal with them now
    __block NSMutableArray *resultArray = [NSMutableArray array];
    [array enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
        obj = [[obj stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]
                    stringByTrimmingCharactersInSet:charSet];
        if ([obj length] > 0)
            [resultArray addObject:obj];
        else
            [resultArray addObject:@"emptySlotInfo"];
    }];
    

    The resultArray contains

    (
    12,
    22,
    stringA,
    emptySlotInfo,
    stringB, stringC,
    2,
    stringD
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to parse following string to get the value 46.4400 INR <div
I need to parse out the string that has following structure x:{a,b,c,}, y:{d,e,f} etc.
I need to parse a string like func1(arg1, arg2); func2(arg3, arg4); . It's not
I am using mechanize/nokogiri and need to parse out the following HTML string. can
I have a string like the following in C#. I need to loop through
I have the following String that I need to parse. I am looking for
Having the following hierarchical text data input (JunOS-like, in fact) I need to parse
I need to parse following String into a DateTime Object: 30/Nov/2009:16:29:30 +0100 Is there
I need to parse the following string into a date object in java: String
I need the most efficient way to parse the following string and extract the

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.