如何在iOS中使用NSRange或NSScanner

时间:2012-05-19 06:47:29

标签: iphone regex nsstring nsattributedstring nsregularexpression

我有一个字符串:

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

每个句子都在数字characters like 1 2 3 4 etc

之间

当我点击第一句时,需要将其解剖并将其复制到剪贴板或其他内容中。我们如何使用NSScanner或NSRange来计算字符串大小或识别数字字符之间的字符串。

self.multiPageView.text = combined;组合包含上面的文字,所以当我点击第一个句子时,它需要选择并显示一个UIAlertView,它符合被点击的句子是第一句话,如何通过敲击该句子,如ibook来检测数字字符之间的句子。我使用核心文本来显示这些类型的文本。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

试试这个你会得到一个数组,其中会有所有分隔的字符串。你必须用你正在使用的数字来填充。

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