简体   繁体   English

如何在iOS中使用NSRange或NSScanner

[英]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 每个句子都位于数字characters like 1 2 3 4 etc之间, 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. 我们如何使用NSScanner或NSRange计算字符串大小或识别数字字符之间的字符串。

self.multiPageView.text =combined; 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. 组合包含上面的文本,因此,当我点击第一个句子时,需要选择并显示一个与所敲击的句子相符的UIAlertView,作为第一个句子,如何通过敲击将该句子(如ibook)固定在数字字符之间的句子。 I am using core text to show these type of text. 我正在使用核心文本来显示这些类型的文本。

Use NSScanner Like - 使用NSScanner

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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