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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:20:52+00:00 2026-06-14T00:20:52+00:00

I would like to parse a string like this: NSString *str = @firstcolumn second

  • 0

I would like to parse a string like this:
NSString *str = @"firstcolumn second column text Third Column Text";

I have three columns of text, each column could be text with spaces.
I know how wide the columns, col1 = 10 chars long, col2 = 20, col3 = 30
I know I could use NSRange(0,len1),(10,len2),(20,len3).

I get crashes ‘Out of range” errors because the length varies, the length of the column text doesn’t have to reach its max limit.

Any ideas how to do this?

NSString *str = @"A000 B11 This is text description This column is a longer Text description"; 
//A000 column can be 10 chars long
//B11 can be 20 chars
//This is some text description can be 30 characters long
 NSString *code1 = [line substringWithRange:NSMakeRange(0,10)];
 NSString *code2 = [line substringWithRange:NSMakeRange(10,20)];
 NSString *shorttext = [line substringWithRange:NSMakeRange(20,20)];
 NSString *longtext = [line substringWithRange:NSMakeRange(30,30)];

I would like to get code1 = A000 in the above example, this can be of length 10 chars long, but don’t have to be as you can see. Same, thing goes for the other 2 columns, code2, and text.
How can I do this?

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

    If I understand correctly, you have an input NSString str which consists of three concatenated strings: col1, col2, and col3. Additionally, you know the following constraints about the problem

    • col1 is between 0 and 10 characters
    • col2 is between 0 and 20 characters
    • col3 is between 0 and 30 characters

    and want to recover these strings from str. Put differently, you want to uniquely determine col1, col2, and col3 so that str is equal to

    [NSString stringWithFormat:@"%@%@%@", col1, col2, col3];
    

    Unfortunately, as others have commented, this is not possible without modifying the problem. To see why not, consider the case where

    str = @"a";
    

    In this case, you know that one of the component strings (col1, col2, or col3) is equal to @"a" and the other two are equal to @"". However, it’s not possible to determine which. If, for example col1 = @"a" and col2 and col3 are both equal to @""; then

    [NSString stringWithFormat:@"%@%@%@", col1, col2, col3]
    

    evaluates to

    @"a"
    

    as desired. However this is also true if col1 and col2 are equal to @"" and col3 = @"a" since

    [NSString stringWithFormat:@"%@%@%@", col1, col2, col3]
    

    still evaluates to

    @"a"
    

    The problem here is not that the component strings are able to be empty but rather that they’re able to vary over a range.

    If we constrained the problem so that the lengths were exact

    • col1, which is 10 characters long
    • col2, which is 20 characters long
    • col3, which is 30 characters long

    it would then be possible to recover str with the following function:

    void GetColumnsFromString(NSString *str, NSString * __autoreleasing *col1, NSString * __autoreleasing *col2, NSString * __autoreleasing *col3)
    {
        if (col1) {
            *col1 = [str substringWithRange:NSMakeRange(0, 10)];
        }
        if (col2) {
            *col2 = [str substringWithRange:NSMakeRange(10, 20)];
        }
        if (col3) {
            *col3 = [str substringWithRange:NSMakeRange(30, 30)];
        }
    }
    

    Another, better, solution, as has been mentioned in the comments, is to use “special” characters in str to demarcate the boundary between the component strings. If we constructed str like this

    str = [NSString stringWithFormat:@"%@%@%@", col1, col2, col3];
    

    and we constrained col1 and col2 and col3 not to contain the character , then we could parse col1 and col2 as follows:

    NSArray *cols = [str componentsSeparatedByString:@""];
    col1 = cols[0];
    col2 = cols[1];
    col3 = cols[2];
    

    The situation is no different if instead of the  character you use the space character.

    Edit: You added more information about the input string and the desired output:

    Rather than three, there are four component strings: col1, col2, col3, and col4. We have some information about them:

    • col1 is between 0 and 10 characters long
    • col1 does not contain the space character
    • col2 is between 0 and 20 characters long
    • col2 does not contain the space character
    • col3 is between 0 and 30 characters long
    • col3 MAY contain the space character
    • col4 isn’t constrained in length
    • col4 MAY contain the space character

    Additionally, the four strings are separated by spaces in their concatenation. So your goal is to uniquely determine col1, col2, col3, and col4 so str is equal to

    [NSString stringWithFormat:@"%@ %@ %@ %@", col1, col2, col3, col4];
    

    You can use an NSScanner to extract col1 and col2 in this case:

    NSScanner *scanner = [NSScanner scannerWithString:str];
    NSCharacterSet *spaceCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@" "];
    NSString *col1 = nil, *col2 = nil;
    [scanner scanUpToCharactersFromSet:spaceCharacterSet intoString:&col1];
    [scanner scanUpToCharactersFromSet:spaceCharacterSet intoString:&col2];
    

    At this point, it’s possible to extract the string remainder which contains the two final strings col3 and col4 separated by a space:

    NSCharacterSet *emptyCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@""];
    NSString *remainder = nil;
    [scanner scanUpToCharactersFromSet:emptyCharacterSet intoString:&remainder];
    

    At this point, you are back in the same sort of situation I described at the beginning. You have a string (remainder) which consists of two component strings (col3 and col4) which are separated by a space. The only way to detect the border between these two strings is that space.

    However, col3 may contain spaces. If it could not, then you could simply scan along until the next space was reached and extract the contents between the beginning and that space into col3 and the rest into col4.

    In addition, col4 may also contain spaces. If it could not, then you could scan from the end of remainder until the first space from the end was reached, extract that range into col4 and the rest into col3.

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

Sidebar

Related Questions

I have a string: [{id:1,gameName:arizona,cost:0.5E1,email:hi@gmail.com,requests:0},{id:2,gameName:arizona,cost:0.5E1,email:hi@gmail.com,requests:0},{id:3,gameName:arizona,cost:0.5E1,email:hi@gmail.com,requests:0}] However, I would like to parse this string into
I have a string like this: key=value, key2=value2 and I would like to parse
I have a text file and I would like to parse it using regular
I have string looking like this: 01 02 03 99 I'd like to parse
I would like to parse a string like this: -o 1 --long Some long
I have string with product information and I would like to parse that string
I have a String which looks like this NSString *string = @2012-04-30T23:59:00+10:00; Right now,
I would like to take a string representation of a set and parse it.
I would like to parse the following text into formatted URLs with anchor tags:
I would like to parse free-text time intervals like the following, using Python: 1

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.