0

With reference to the following question "String contains string in objective-c", I have a HTTP server that returns the following header:

P.S. Cannot attach a screenshot because I'm a new user, used blockquotes instead :(

HTTP header: {

-info omitted-

Server = "HTTP Client Suite (Test case number:21)";

The block of code which i have written to get a response from the HTTP server is:

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    *- code omitted -*

     // **HTTP header field**

    // A dictionary containing all the HTTP header fields of the receiver
    // By examining this dictionary clients can see the β€œraw” header information returned by the server
    NSDictionary *headerField = [[NSDictionary alloc]initWithDictionary:[(NSHTTPURLResponse *)httpResponse allHeaderFields]];

    // Call headerField dictionary and format into a string
    NSString *headerString = [NSString stringWithFormat:@"%@", headerField];

    NSLog(@"HTTP Header: %@",headerString);

    // String to match (changeable) and temporary string to store variable
    NSString *stringToMatch = @"Test case number";
    NSString *tempString = @"";

    // Check if headerString contains a particular string
    // By finding and returning the range of the first occurrence of the given string (stringToMatch) within the receiver
    // If the string is not found/doesn't exists
    if ([headerString rangeOfString:stringToMatch].location == NSNotFound)
    {
        NSLog(@"Header does not contain the string: '%@'", stringToMatch);
        tempString = NULL;
        NSLog(@"String is %@", tempString);
    }
    else
    {
        NSLog(@"Header contains the string: '%@'", stringToMatch);
        tempString = stringToMatch;
        NSLog(@"String is '%@'", tempString);
    }

}

What I'm doing here is to actually see if the string "Test case number" exists. If it does, then I would want to extract the number 21 and store it in a variable using NSInteger, the problem now is the number is mutable and not constant (it will change each time depending on what the HTTP server returns), therefore I'm unable to use the same approach which I did earlier on to check whether an integer exists within a string in this case.

How do I go about achieving this? Thanks in advance!

Community
  • 1
  • 1
Dawson
  • 117
  • 1
  • 9

1 Answers1

0

You can use a regular expression to extract the number from the string. The pattern I'm using here is "Test case number:<integer>".

First create a regular expression:

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"Test case number:(\\d+)"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];

Check if your string matches this regular expression:

NSString *string = @"HTTP Client Suite (Test case number:21)";
NSTextCheckingResult *match = [regex firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];

If a match was found, then extract it from the original string. First get the range (starting position, length) of the number from the original string, and then use substringWithRange: on the original string to extract the number. It's a little verbose :)

if (match) {
    NSRange range = [match rangeAtIndex:1];
    NSString *number = [string substringWithRange:range];
}

Now number should contain the number you're looking for as a string.

Anurag
  • 140,337
  • 36
  • 221
  • 257
  • hi I have a problem with your code, somehow it only seems to return "2" instead of "21", it is the same when the number is for e.g. "414", it'll only return "4" – Dawson Nov 06 '12 at 01:24
  • Good catch. The regular expression should've been `\\d+` instead of `\\d`. Fixed now. The `+` implies that were are looking for one or more digits. Without it we're just looking for a single digit. – Anurag Nov 06 '12 at 20:25