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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:42:38+00:00 2026-05-19T22:42:38+00:00

I have a {Red,Blue,Green,Yellow} returned as string. How to process this to add to

  • 0

I have a {“Red”,”Blue”,”Green”,”Yellow”} returned as string. How to process this to add to an array ?

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSString* sampleString = @"{\"Red\",\"Blue\",\"Green\",\"Yellow\"}";
    NSArray* components = [sampleString componentsSeperatedByString:@"\"{"];

    [pool drain];
    return 0;
}

Updated Code#

NSString* sampleString = @"{\"Red\",\"Blue\",\"Green\",\"Yellow\"}";

NSMutableArray *rows = [NSMutableArray array];

// Get newline character set
NSMutableCharacterSet *removeCharacterSet = (id)[NSMutableCharacterSet characterSetWithCharactersInString:@"{(,}"];
[removeCharacterSet formIntersectionWithCharacterSet:[[NSCharacterSet whitespaceCharacterSet] invertedSet]];

// Characters that are important to the parser
NSMutableCharacterSet *importantCharactersSet = (id)[NSMutableCharacterSet characterSetWithCharactersInString:@"\""];
[importantCharactersSet formUnionWithCharacterSet:removeCharacterSet];

// Create scanner, and scan string
NSScanner *scanner = [NSScanner scannerWithString:sampleString];
[scanner setCharactersToBeSkipped:nil];
while ( ![scanner isAtEnd] ) 
{        
    BOOL insideQuotes = NO;
    BOOL finishedRow = NO;
    NSMutableArray *columns = [NSMutableArray arrayWithCapacity:10];
    NSMutableString *currentColumn = [NSMutableString string];
    while ( !finishedRow ) 
    {
        NSString *tempString;
        if ( [scanner scanUpToCharactersFromSet:importantCharactersSet intoString:&tempString] ) {
            [currentColumn appendString:tempString];
        }

        if ( [scanner isAtEnd] ) {
            if ( ![currentColumn isEqualToString:@""] ) [columns addObject:currentColumn];
            finishedRow = YES;
        }
        else if ( [scanner scanCharactersFromSet:removeCharacterSet intoString:&tempString] ) {
            if ( insideQuotes ) {
                // Add line break to column text
                [currentColumn appendString:tempString];
            }
            else {
                // End of row
                if ( ![currentColumn isEqualToString:@""] ) [columns addObject:currentColumn];
                finishedRow = YES;
            }
        }
        else if ( [scanner scanString:@"\"" intoString:NULL] ) {
            if ( insideQuotes && [scanner scanString:@"\"" intoString:NULL] ) {
                // Replace double quotes with a single quote in the column string.
                [currentColumn appendString:@"\""]; 
            }
            else {
                // Start or end of a quoted string.
                insideQuotes = !insideQuotes;
            }
        }
        else if ( [scanner scanString:@"," intoString:NULL] ) {  
            if ( insideQuotes ) {
                [currentColumn appendString:@","];
            }
            else {
                // This is a column separating comma
                [columns addObject:currentColumn];
                currentColumn = [NSMutableString string];
                [scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:NULL];
            }
        }
    }
    if ( [columns count] > 0 ) [rows addObject:columns];
}
NSLog(@"This String:%@",[rows objectAtIndex:0]);

I got code from http://www.macresearch.org/cocoa-scientists-part-xxvi-parsing-csv-data. Now the output is This String:( Red ), How to get rid of “(” “)” ?

  • 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-05-19T22:42:39+00:00Added an answer on May 19, 2026 at 10:42 pm

    Here’s all you need to scan the sample you’ve provided using an instance of NSScanner:

    NSScanner *scanner = [NSScanner scannerWithString:@"{\"Red\",\"Blue\",\"Green\",\"Yellow\"}"];
    NSMutableCharacterSet *charactersToSkip = [NSMutableCharacterSet punctuationCharacterSet];
    [scanner setCharactersToBeSkipped:charactersToSkip]; 
    NSMutableArray *substrings = [NSMutableArray array];
    NSString *substring = @"";
    while (![scanner isAtEnd]) {
        [scanner scanUpToCharactersFromSet:charactersToSkip intoString:&substring];
        [scanner scanCharactersFromSet:charactersToSkip intoString:NULL];
        [substrings addObject:substring];
    }
    
    NSLog(@"%@", substrings);
    

    Note that if you substituted parens for curly braces, all you’d need to do to create an array of strings from the sample would be:

    NSString *sampleString = @"(\"Red\",\"Blue\",\"Green\",\"Yellow\")";
    NSArray *strings = [sampleString propertyList];
    NSLog(@"%@", strings);
    

    …but I’m not really clear on what you need to accomplish.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two array lists dim Colors1 = New ArrayList Colors1.Add(Blue) Colors1.Add(Red) Colors1.Add(Yellow) Colors1.Add(Green)
I have a tab-delimited list of pairs like this: apple yellow orange green apple
I have 1 red polygon say and 50 randomly placed blue polygons - they
I have a very simple html. The red div is inside the blue div
I have to mix some colors in a natural way. This means blue +
I have an enum like: public enum Blah { RED = 2, BLUE =
No, not literally . Rather, do you have any red tape horror stories of
I have a big red button and I'm trying to use javascript to perform
I have some data that looks something like this... +----------+----------+----------+ | Column 1 |
I have a few questions for this program, one the first thing I am

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.