日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 搜索关键字字符串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 scanstringreturn scanstringexcept 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 pathfrom simplejson._speedups import scanstringreturn scanstringexcept 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'`` bydefault). 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 everyJSON object decoded and its return value will be used in place of thegiven :class:`dict`. This can be used to provide customdeserializations (e.g. to support JSON-RPC class hinting).*object_pairs_hook* is an optional function that will be called withthe 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 decodersthat rely on the order that the key and value pairs are decoded (forexample, :func:`collections.OrderedDict` will remember the order ofinsertion). If *object_hook* is also defined, the *object_pairs_hook*takes priority.*parse_float*, if specified, will be called with the string of everyJSON float to be decoded. By default, this is equivalent to``float(num_str)``. This can be used to use another datatype or parserfor JSON floats (e.g. :class:`decimal.Decimal`).*parse_int*, if specified, will be called with the string of everyJSON int to be decoded. By default, this is equivalent to``int(num_str)``. This can be used to use another datatype or parserfor JSON integers (e.g. :class:`float`).*parse_constant*, if specified, will be called with one of thefollowing strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``. Thiscan be used to raise an exception if invalid JSON numbers areencountered.*strict* controls the parser's behavior when it encounters aninvalid 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 = encodingself.object_hook = object_hookself.object_pairs_hook = object_pairs_hookself.parse_float = parse_float or floatself.parse_int = parse_int or intself.parse_constant = parse_constant or _CONSTANTS.__getitem__self.strict = strictself.parse_object = JSONObjectself.parse_array = JSONArrayself.parse_string = scanstring self.memo = {}self.scan_once = make_scanner(self)

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

總結

以上是生活随笔為你收集整理的搜索关键字字符串NSSCanner:scanString()详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。