简体   繁体   中英

How to use NSRange or NSScanner in iOS

I have a string like:

1 this is my first text 2 his is my second 3 this is my third 4 this is my forth etc etc..

Each sentence is between the numeric characters like 1 2 3 4 etc

When I tap the first sentence, it needs to dissect it and copy it in clipboard or something. How can we use he NSScanner or NSRange to calculate the string size or identify strings between the numeric characters.

self.multiPageView.text =combined; the combined contains the text like above ,,so when i tap the first sentence it need to select and show a UIAlertView which conforms the tapped sentence is first sentence,how to dectected a sentence between numeric characters by tap pin that sentence,like ibook. I am using core text to show these type of text.

Use NSScanner Like -

NSString *yourString = @"1 this is my first text 2 his is my second 3 this is my third 4 this is my forth";

NSScanner *scanner = [NSScanner scannerWithString:yourString];

[scanner scanUpToString:@"1" intoString:nil];
if (![scanner isAtEnd]) {
    [scanner scanUpToString:@"1" intoString:nil];
    NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@"2"];
    [scanner scanUpToCharactersFromSet:charset intoString:&yourString];
}

Try this you will get array in which there will be all seperated strings. You have to fill set with numbers you are using.

    NSString *str1 = @"1 this is my first text 2 his is my second 3 this is my third 4 this is my forth";
    NSMutableCharacterSet *set = [NSMutableCharacterSet controlCharacterSet];
    [set addCharactersInString:@"1"];
    [set addCharactersInString:@"2"];
    [set addCharactersInString:@"3"];
    [set addCharactersInString:@"4"];
    NSArray *arr = [str1 componentsSeparatedByCharactersInSet:set];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM