NSScanner是一个类,用于在字符串中扫描指定的字符,尤其是把它们翻译/转换为数字和别的字符串。可以在创建NSScaner时指定它的string属性,然后scanner会按照你的要求从头到尾地扫描这个字符串的每个字符。
属性:
@property (readonly, copy) NSString *string; // 初始化时赋给NSScanner的字符串
@property NSUInteger scanLocation; // NSScanner扫描的起始位置
@property (copy) NSCharacterSet *charactersToBeSkipped; //扫描的时候需要忽略的值
@property
BOOL caseSensitive; //是否区分大小写
@property (retain)
id locale; //大概意思是不同语言环境解释字符串的方式不一样
@property (getter=isAtEnd,
readonly) BOOL atEnd; // 是否扫描完了
方法:
// 以下三个方法均可以用来初始化一个NSScanner对象
- (instancetype)initWithString:(NSString *)string NS_DESIGNATED_INITIALIZER;
+ (instancetype)scannerWithString:(NSString *)string;
+ (id)localizedScannerWithString:(NSString *)string;
// On overflow, the below methods will return success and clamp
// 用来查找NSScanner对象中的相应一般对象(int型,double型等等)
- (BOOL)scanInt:(int *)result;
- (BOOL)scanInteger:(NSInteger *)result NS_AVAILABLE(10_5,
2_0);
- (BOOL)scanLongLong:(long
long *)result;
- (BOOL)scanUnsignedLongLong:(unsigned
long long *)result NS_AVAILABLE(10_9,
7_0);
- (BOOL)scanFloat:(float *)result;
- (BOOL)scanDouble:(double *)result;
- (BOOL)scanHexInt:(unsigned *)result;
// Optionally prefixed with "0x" or "0X"
- (BOOL)scanHexLongLong:(unsigned
long long *)result NS_AVAILABLE(10_5,
2_0); // Optionally prefixed with "0x" or "0X"
- (BOOL)scanHexFloat:(float *)result NS_AVAILABLE(10_5,
2_0);
// Corresponding to %a or %A formatting. Requires "0x" or"0X"
- (BOOL)scanHexDouble:(double *)result NS_AVAILABLE(10_5,
2_0);
// Corresponding to %a or %A formatting. Requires "0x" or "0X"
// 用来查找NSScanner中的指定字符串的对象
- (BOOL)scanString:(NSString *)string intoString:(NSString **)result; //从scan中扫描出set中的数据放入value中
- (BOOL)scanCharactersFromSet:(NSCharacterSet *)set intoString:(NSString **)result; //从scan中扫描出set中的数据放入value中
- (BOOL)scanUpToString:(NSString *)string intoString:(NSString **)result; //从scan中扫描出set之外的数据放入value中
- (BOOL)scanUpToCharactersFromSet:(NSCharacterSet *)set intoString:(NSString **)result; //从scan中扫描出set之外的数据放入value中
使用:
1.查找一个字符串中所有的int型
NSString *str =
@"23.42ksjfk5k35j3kjsdkjks53k5j3k";
NSScanner *scanner = [NSScanner
scannerWithString:str];
// 创建一个NSScanner的对象
int num =
0;
while (scanner.isAtEnd ==
NO) {
// 如果有找到匹配的int型,scanLocation会自动跳转到最后一个数字的后面,如果没有找到,则scanLocation不会变化,要手动跳转
if ([scanner
scanInt:&num]) { //
如果有找到int型的数字,将其赋值给num
NSLog(@"%i", num);
} else {
// 如果没有找到,将scanner的scanLocation++
scanner.scanLocation++;
}
}
2.在iOS中使用HTML格式的字符串,去掉所有<>中的内容
NSString *str =
@"我爱你<lalala>大发<大大发>《》<adsfad>asfd";
NSScanner *scanner = [NSScanner
scannerWithString:str];
// 初始化一个NSScanner对象
NSString *text =
@""; // 用来存储查询到的字符串
while (scanner.isAtEnd ==
NO) { // 判断是否扫描完毕
[scanner scanUpToString:@"<"
intoString:nil];
// 查找字符串中的@"<"子串,并将scanLocation停留在@"<"的首个字符的位置上
[scanner scanUpToString:@">"
intoString:&text];
// 查找字符串中的@">"子串,并截取@"<"到@">"的子串(不包括@">")
//
将扫描到的<>中的内容全部替换成@""
str = [str stringByReplacingOccurrencesOfString:[NSString
stringWithFormat:@"%@>", text]
withString:@""];
}
NSLog(@"%@", str);
3.输出所有<>中的内容
NSString *str =
@"我爱你<lal ala>
大发 <大大发>《》<adsfad>asfd";
NSScanner *scanner = [NSScanner
scannerWithString:str];
// 初始化一个NSScanner对象
NSString *text =
@""; // 用来存储查询到的字符串
while (scanner.isAtEnd ==
NO) { // 判断是否扫描完毕
[scanner scanUpToString:@"<"
intoString:nil];
// 查找字符串中的@"<"子串,并将scanLocation停留在@"<"的首个字符的位置上
[scanner
scanString:@"<"
intoString:nil];
[scanner scanUpToString:@">"
intoString:&text];
// 查找字符串中的@">"子串,并截取@"<"到@">"的子串(不包括@">")
NSLog(@"%@",
text);
}
4.从一个字符串中读取出规定的子串
假设你有如下字符串:
Product: Acme Potato Peeler; Cost: 0.98 73
Product: Chef Pierre Pasta Fork; Cost: 0.75 19
Product: Chef Pierre Colander; Cost: 1.27 2
以下代码演示了读取产品名称和价格的操作(价格简单地读作一个float),跳过“Product:”和“Cost:"子串,以及分号。注意,因为scanner默认忽略空白字符和换行符,循环中没有指定对它们的处理(尤其对于读取末尾的整数而言,并不需要处理额外的空白字符)
NSString *string =
@"Product: Acme Potato Peeler; Cost: 0.98 73\n\
Product: Chef Pierre Pasta Fork; Cost: 0.75 19\n\
Product: Chef Pierre Colander; Cost: 1.27 2\n";
NSCharacterSet *semicolonSet;
NSScanner *theScanner;
NSString *PRODUCT =
@"Product:";
NSString *COST =
@"Cost:";
NSString *productName;
float productCost;
NSInteger productSold;
semicolonSet = [NSCharacterSet
characterSetWithCharactersInString:@";"];
theScanner = [NSScanner
scannerWithString:string];
while ([theScanner
isAtEnd] == NO)
{
if ([theScanner scanString:PRODUCT
intoString:NULL] &&
[theScanner scanUpToCharactersFromSet:semicolonSet intoString:&productName] &&
[theScanner scanString:@";"
intoString:NULL] &&
[theScanner scanString:COST
intoString:NULL] &&
[theScanner scanFloat:&productCost] &&
[theScanner scanInteger:&productSold])
{
NSLog(@"Sales of %@: $%1.2f", productName, productCost * productSold);
}
}