如何在iOS中使用NSRange或NSScanner [英] How to use NSRange or NSScanner in iOS

查看:50
本文介绍了如何在iOS中使用NSRange或NSScanner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的字符串:

I have a string like:

1这是我的第一个文本2他是我的第二个文本3这是我的第三个文本这是我的第四个等等.

每个句子都位于数字字符之间,例如1 2 3 4等

当我点击第一个句子时,需要对其进行剖析并将其复制到剪贴板中.我们如何使用NSScanner或NSRange计算字符串大小或识别数字字符之间的字符串.

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

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.

推荐答案

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

这篇关于如何在iOS中使用NSRange或NSScanner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆