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

  • Home
  • SEARCH
  • 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 7866655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:27:52+00:00 2026-06-03T00:27:52+00:00

Although iOS provides a lot of useful string methods, I can’t find a good

  • 0

Although iOS provides a lot of useful string methods, I can’t find a good solution for getting ranges of strings that are separated by a given character.

original string:

|You| will |achieve| everything you |want| if you |work| hard

separateness character is |.

separate string1: You (range: 3, 3)

separate string2: achieve (range: 12, 7)

separate string3: want (range: 37, 4)

separate string4: work (range: 51, 4)

NSString method’s substringFromIndex: makes it possible to find these ranges using NSString, but this seems inefficient.

Please, let me know the better ways to solve 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-03T00:27:54+00:00Added an answer on June 3, 2026 at 12:27 am

    You should use NSRegularExpression‘s matchesInString:options:range: method.

    Return Value

    An array of NSTextCheckingResult objects. Each result
    gives the overall matched range via its range property, and the range
    of each individual capture group via its rangeAtIndex: method. The
    range {NSNotFound, 0} is returned if one of the capture groups did not
    participate in this particular match.

    You might have code like:

    NSString *str = @"|You| will |achieve| everything you |want| if you |work| hard";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:
        @"[^|]*" options: 0 error:nil];
    NSArray *results = [regex matchesInString:str 
                                      options:0 
                                        range:NSMakeRange(0, [str length])];
    // ... do interesting code on results...
    // Note that you should iterate through the array and use the 'range' property
    // to get the range.
    
    for (NSTextCheckingResult *textResult in results)
    {
       if (textResult.range.length > 0)
       {
          NSString *substring = [myStr substringWithRange:textResult.range];
          NSLog(@"string at range %@ :: \"%@\"", 
                NSStringFromRange(textResult.range), 
                substring);
       }
    }
    

    Logs:

    string at range {1, 3} :: “You”

    string at range {5, 6} :: ” will “

    string at range {12, 7} :: “achieve”

    string at range {20, 16} :: ” everything you “

    string at range {37, 4} :: “want”

    string at range {42, 8} :: ” if you “

    string at range {51, 4} :: “work”

    string at range {56, 5} :: ” hard”

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

Sidebar

Related Questions

Although this is FAQ i still couldn't find a suitable solution. I'd like to
Although obviously not all scenarios can be covered by a single design, is it
Although there are a lot of posts about .net config files, I believe that
How can I add a timer to my iOS app that is based on
Im really new to iOS Programming. Although I read lots of books and material
Although I have no problem whatsoever understanding recursion, I can't seem to wrap my
I'm new to iOS programming, and although I understand ObjC, It seems to me
Note: Although I raise this issue in the context of an iOS app, I
On iOS there is this nice feature that an app developer can specify device-specific
I'm developing an iOs 5 app where you can paint on a UIImageView inside

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.