Siva
Siva

Reputation: 569

Formatting an String

I have an output string in this format . I need to format the string such that i can display the URL separately and my Content, the description separately. Is there any functions , so i can format them easily ? The code :

            NSLog(@"Description %@", string);

The OUTPUT String:

    2013-07-28 11:13:59.083 RSSreader[4915:c07] Description         
    http://www.apple.com/pr/library/2013/07/23Apple-Reports-Third-Quarter-Results.html?sr=hotnews.rss
    Apple today announced financial results for its fiscal 2013 third quarter ended
    June     29, 2013. The Company posted quarterly revenue of $35.3 billion and quarterly 
    net profit of $6.9 billion, or $7.47 per diluted share. 
    Apple sold 31.2 million iPhones, which set a June quarter record. 

Upvotes: 0

Views: 116

Answers (3)

Roshit
Roshit

Reputation: 1579

Regex is a good idea.

But there is a default way of detecting URLs within a String in Objective C, NSDataDetector. NSDataDetector internally uses Regex.

NSString *aString = @"YOUR STRING WITH URLs GOES HERE"
NSDataDetector *aDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *theMatches = [aDetector matchesInString:aString options:0 range:NSMakeRange(0, [aString length])];
for (int anIndex = 0; anIndex < [theMatches count]; anIndex++) {
    // If needed, Save this URL for Future Use.
    NSString *anURLString = [[[theMatches objectAtIndex:anIndex] URL] absoluteString];

    // Replace the Url with Empty String
    aTitle = [aTitle stringByReplacingOccurrencesOfString:anURLString withString:@""];
}

Upvotes: 0

Sergei Belous
Sergei Belous

Reputation: 5038

If description string separated by line break (\n), you can do this:

NSArray *items = [yourString componentsSeparatedByString:@"\n"];

Upvotes: 0

AVEbrahimi
AVEbrahimi

Reputation: 19214

You should extract URL from string, then display it in formatted way.

A simple way to extracting URL is regular expressions (RegEX).

After extracting URL you can replace it with nothing:

str = [str stringByReplacingOccurrencesOfString:extractedURL
                                     withString:@""];

You can use this : https://stackoverflow.com/a/9587987/305135

Upvotes: 1

Related Questions