搜索关键字字符串NSSCanner:scanString()详解

搜索关键字字符串NSSCanner:scanString()用来查找某段文本中某个关键字出现的位置。
NSScanner类用于在字符串中扫描指定的字符,尤其是把它们翻译/转换为数字和别的字符串。可以在创建NSScaner时指定它的string属性,然后scanner会按照你的要求从头到尾地扫描这个字符串的每个字符。
scanner默认情况下会忽略空白字符和换行符。注意:对于忽略字符,大小写是敏感的。

看下面的代码:

(NSString*)extractBodyFromMessage:(NSString* )msg{

    NSString* body=msg;

    NSString* keyString=@"【来自网易邮箱的超大附件】";

    NSScanner*scanner=[NSScanner scannerWithString:body];

    [scannersetCaseSensitive:NO];

    BOOL b;

    while (![scanner isAtEnd]){

        b=[scannerscanString:keyString intoString:NULL];

        if(b) {

           body=[body substringToIndex:[scanner scanLocation]-keyString.length];

           break;

       }else{

           scanner.scanLocation++;

        }

    }

    return body;

}

在while循环里面,我们首先从0位置开始扫描指定关键字,如果未找到,移动scanLocation位置,继续从下一字符查找直至文本末尾;如果找到,直接返回子字符串。

scanUpToString在找到指定文本(keyString)后,会将scanLocation停止在keyString之前(scanString方法会将scanLocation移到keyString之后位置),这样我们的substringToIndex:方法就不必再用scanLocation减去keyString的长度了:
body=[bodysubstringToIndex:[scanner scanLocation]];

其中,如果scanUpToString在文本的开头(第一个字符)就找到目标文本,会返回NO,stringValue不会改变。

如果目标文本在receiver文本中被找到,返回YES并将scanLocation设置到已找到的目标文本之前。

如果目标文本未找到,从本次查找开始剩下的整个receiver文本被放入stringValue,scanLocation移动到源串的末尾,返回YES。
*
scanUpToString和scanString的区别*,
如果目标文本一开头就出现目标文本(scanner默认是忽略空格和换行的,因此如果开头有这两个空白字符,scanner视若未见),二者将得到完全不同的结果:scanString会返回YES,而scanUpToString返回NO。
下面是scanstring方法的几个示例:

# 需要导入模块: from simplejson import _speedups [as 别名]
# 或者: from simplejson._speedups import scanstring [as 别名]
def _import_c_scanstring():
    try:
        from simplejson._speedups import scanstring
        return scanstring
    except ImportError:
        return None 
# 需要导入模块: from simplejson import _speedups [as 别名]
# 或者: from simplejson._speedups import scanstring [as 别名]
def _import_c_scanstring():
    try:
        raise ImportError # because assumes simplejson in path
        from simplejson._speedups import scanstring
        return scanstring
    except ImportError:
        return None 
# 需要导入模块: from simplejson import _speedups [as 别名]
# 或者: from simplejson._speedups import scanstring [as 别名]
def __init__(self, encoding=None, object_hook=None, parse_float=None,
            parse_int=None, parse_constant=None, strict=True,
            object_pairs_hook=None):
        """
        *encoding* determines the encoding used to interpret any
        :class:`str` objects decoded by this instance (``'utf-8'`` by
        default).  It has no effect when decoding :class:`unicode` objects.

        Note that currently only encodings that are a superset of ASCII work,
        strings of other encodings should be passed in as :class:`unicode`.

        *object_hook*, if specified, will be called with the result of every
        JSON object decoded and its return value will be used in place of the
        given :class:`dict`.  This can be used to provide custom
        deserializations (e.g. to support JSON-RPC class hinting).

        *object_pairs_hook* is an optional function that will be called with
        the result of any object literal decode with an ordered list of pairs.
        The return value of *object_pairs_hook* will be used instead of the
        :class:`dict`.  This feature can be used to implement custom decoders
        that rely on the order that the key and value pairs are decoded (for
        example, :func:`collections.OrderedDict` will remember the order of
        insertion). If *object_hook* is also defined, the *object_pairs_hook*
        takes priority.

        *parse_float*, if specified, will be called with the string of every
        JSON float to be decoded.  By default, this is equivalent to
        ``float(num_str)``. This can be used to use another datatype or parser
        for JSON floats (e.g. :class:`decimal.Decimal`).

        *parse_int*, if specified, will be called with the string of every
        JSON int to be decoded.  By default, this is equivalent to
        ``int(num_str)``.  This can be used to use another datatype or parser
        for JSON integers (e.g. :class:`float`).

        *parse_constant*, if specified, will be called with one of the
        following strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``.  This
        can be used to raise an exception if invalid JSON numbers are
        encountered.

        *strict* controls the parser's behavior when it encounters an
        invalid control character in a string. The default setting of
        ``True`` means that unescaped control characters are parse errors, if
        ``False`` then control characters will be allowed in strings.

        """
        self.encoding = encoding
        self.object_hook = object_hook
        self.object_pairs_hook = object_pairs_hook
        self.parse_float = parse_float or float
        self.parse_int = parse_int or int
        self.parse_constant = parse_constant or _CONSTANTS.__getitem__
        self.strict = strict
        self.parse_object = JSONObject
        self.parse_array = JSONArray
        self.parse_string = scanstring 
        self.memo = {}
        self.scan_once = make_scanner(self) 

参考:
博客1地址
参考2地址
参考3地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值