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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:05:14+00:00 2026-06-18T17:05:14+00:00

I am trying to parse a tweet that may contain a url. I want

  • 0

I am trying to parse a tweet that may contain a url. I want to format the url with a different color/font within the rest of the tweet. So obviously, the url can be placed anywhere in the text. So I have a category method that looks like this :

NSString *urlString = @"";
NSRange startRange = [self rangeOfString:@"http://" options:NSCaseInsensitiveSearch];

if (startRange.length) {
    NSString *subStr = [self substringFromIndex:startRange.location];
    NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSRange endRange = [subStr rangeOfCharacterFromSet:set];

    if (endRange.length) {
        NSRange finalRange;
        finalRange.location = startRange.location;
        finalRange.length = endRange.location - 1;
        NSString *finalString = [self substringWithRange:finalRange];
        debugTrace(@"finalString : %@", finalString);
    }
}

return urlString;

So basically I find the range of http:// in the text and then make a substring from there to the end. From that I look for the first whitespaceAndNewlineCharacterSet and use that as the end of url to find the finalString. However, the line

NSRange endRange = [subStr rangeOfCharacterFromSet:set];

returns garbage value, which makes me think that I am not using the correct NSCharacterSet to find the endRange

Printing description of endRange:
(NSRange) endRange = location=2147483647, length=0

How can I go about this so that I find where the URL has ended.

  • 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-18T17:05:15+00:00Added an answer on June 18, 2026 at 5:05 pm

    As mentioned in my comment, it’s not a garbage range value, it’s a representation for NSNotFound. This means there isn’t any occurrence of whitespace or newlines in the string you have passed it (which is a valid case). As such, we need to prepare for that.

    Based on your code, I have changed it to check for NSNotFound. Also, there was no need to subtract 1 from the finalRange length. When I tested this code sample, that cropped the last character from my URL string. Comments have been added to the end of changed/queried lines.

    NSString *urlString = @""; // Is this being used?
    NSRange startRange = [self rangeOfString:@"http://" options:NSCaseInsensitiveSearch];
    
    if (startRange.length) {
        NSString *subStr = [self substringFromIndex:startRange.location];
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSRange endRange = [subStr rangeOfCharacterFromSet:set];
    
        if (endRange.location == NSNotFound) { // Check for NSNotFound
            debugTrace(@"finalString : %@", subStr); 
        }
        else if (endRange.length) {
            NSRange finalRange;
            finalRange.location = startRange.location;
            finalRange.length = endRange.location; // No need to subtract 1
            NSString *finalString = [self substringWithRange:finalRange];
            debugTrace(@"finalString : %@", finalString);
        }
    }
    
    return urlString;
    

    I have tested this briefly, and it seems to work.

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

Sidebar

Related Questions

Im trying to parse an html output from a url call but i found
I'm trying parse html with Jsoup lib. Everything works perfect, but something that does't
I am trying parse out some text that is in an html file. The
I trying to parse a data in a MySql Format, I ran across SimpleDateFormat
I'm trying to parse a text file that has a heading and the body.
Im trying to parse a Xml file from a URL, the xml is actually
I'm developing an application and I need to parse a Tweet timestamp that actually
I am trying parse a map file that is generated by gcc, for function
I'm trying parse a html form that looks like that: <select name=country> <option value=1>Afghanistan</option>
I'm trying parse the follow XML file: <root>Root <pai>Pai_1 <filho>Pai1,Filho1</filho> <filho>Pai1,Filho2</filho> </pai> <pai>Pai_2 <filho>Pai2,Filho1</filho>

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.