How to use NSRange or NSScanner in iOS

流过昼夜 提交于 2019-12-13 07:14:34

问题


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.


回答1:


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];
}



回答2:


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];


来源:https://stackoverflow.com/questions/10663014/how-to-use-nsrange-or-nsscanner-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!