iOS NSScanner

      NSString *string = @"My age is d 23 34.0 now i will ,get off work!my name is wang tian :qiao";
    int i;
    float f;
    NSString *theString = @"My age is";
    NSString *resultString;
    NSString *dString;
    
    //扫描字符串------函数:scanString
    NSScanner *theScanner = [NSScanner scannerWithString:string ];
    
    //设置是否忽略大小写,如果设置忽略大小写,那么不符合扫描规则的,将不会进行扫描。默认请看
        //   [theScanner setCaseSensitive:NO];
    
    //扫描时,忽略某一个字符"z".
   // [theScanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"z"]];
    
    //扫描“theString”字符串
           [theScanner scanString:theString intoString:&resultString];
    NSLog(@"resultString is %@",resultString); 
     
    //扫描字符串中的d字符-函数:scanString
    [theScanner scanString:@"d" intoString:&dString];
    NSLog(@"d string is %@",dString);
    
    //扫描字符串中的整型数据  --函数:scanInteger
    [theScanner scanInteger:&i];
    NSLog(@"intergent is %d",i);
    
      //扫描字符串中的浮点型数据  --函数:scanFloat
    [theScanner scanFloat:&f];
    NSLog(@"float is %f",f);
    
   //将十六进制的字符串转化为double类型的数据。 --函数:scanHexDouble  输出会出现问题。
    double HexDouble;
    [[NSScanner scannerWithString:@"A1" ]scanHexDouble:&HexDouble];

    NSLog(@"the Hex is EE is Equal doubleValue:%f",HexDouble);
    
    //将十六进制的字符串转化为int类型的数据。 --函数:scannerWithString  
    unsigned int HexInt;
    [[NSScanner scannerWithString:@"EE" ]scanHexInt:&HexInt];
    NSLog(@"the Hex is EE is Equal intValue:%d",HexInt);

    //创建一个字符集,并扫描到这个字符集之前并保存扫描过的字符:    --函数:scanUpToCharactersFromSet。
    NSString *charaString;
    NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@","];
    [theScanner scanUpToCharactersFromSet:characterSet intoString:&charaString];
    NSLog(@"the charaString is %@",charaString);
    
    //过滤掉某一些字符比如“,”号    函数:scanString
    [theScanner scanString:@"," intoString:NULL];
    
    //扫描字符串,在碰到指定的字符串时,获取其之前的字符数据。但不包含给定的字符,比如“k!” --函数:scanUpToString。
    NSString *scanUpToString ;
    [theScanner scanUpToString:@"k!" intoString:&scanUpToString];
    NSLog(@"scanUpToString is %@",scanUpToString);
    
    
    //设定下一次的扫描位置:并验证是否正确。 --函数:scanUpToCharactersFromSet
    [theScanner setScanLocation:50];
    NSString *ccString;
    NSCharacterSet *rSet = [NSCharacterSet characterSetWithCharactersInString:@":"];
    [theScanner scanUpToCharactersFromSet:rSet intoString:&ccString];
    NSLog(@"ScanLocation's string %@",ccString);
    
    //返回正在扫描的位置  函数:position
    
    NSUInteger position;
    position  = [theScanner scanLocation];
    NSLog(@"scanLocation is %d",position);
    
    //返回扫描的字符串   --函数:string

    NSString *scanString = [theScanner string];
    NSLog(@"string is %@",scanString);
    
    

你可能感兴趣的:(iOS NSScanner)