Ethan Allen
Ethan Allen

Reputation: 14835

How can I get the count of how many times a string is inside a long NSString?

I have the following long NSString:

    <span class='page-numbers current'>1</span>
    <a class='page-numbers' href='http://www.testurl1.com'>2</a>
    <a class='page-numbers' href='http://www.testurl2.com'>3</a>
    <a class='page-numbers' href='http://www.testurl3.com'>4</a>

I am trying to find out how many times the string page-numbers is in this NSString. Is there a simple method to do this?

Upvotes: 0

Views: 90

Answers (3)

Wubao Li
Wubao Li

Reputation: 1748

use NSRegularExpression, is more efficient and clear.

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"page-numbers"
                                                           options:NSRegularExpressionCaseInsensitive error:&error];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:string
                                                    options:0
                                                      range:NSMakeRange(0, [string length])];

Upvotes: 0

RAJA
RAJA

Reputation: 1214

Try to use the below code to get the count

NSArray *array = [string componentsSeparatedByString:@"page-numbers"];
int count = [array count]-1;

I think the above code will be useful to you..

Upvotes: 5

iWill
iWill

Reputation: 145

you can do that with some JS scripts.

eg: documents getelementbyid

then , you can get the count.

ps: js script should run in a webview.

Upvotes: 0

Related Questions