美文网首页
NSString (API+常用实例)

NSString (API+常用实例)

作者: 超人_22 | 来源:发表于2018-06-02 11:24 被阅读0次

    iOS 字符串常用方法

    -(void)teset{
    //初始化

    NSString* str = [NSString stringWithFormat:@"asdfsdfvvvvvvvas"];
    NSString* str2 = @"110A";
    NSString* str3 = @"110a";
    
    
    
    //字符串长度
    //@lengh (readonly)
    NSLog(@"字符串长度:%ld",(long)str.length);
    
    
    
    //获取字符串某位
    unichar c = [str characterAtIndex:1];
    
    
    
    //根据下标和长度截取字符串
    NSRange rang = {6,8};
    NSString* str4 = [str substringWithRange:rang];
    NSLog(@"截取字符串:%@",str4);
    
    
    
    //从指定下标截取到最后
    NSString* str5 = [str substringFromIndex:3];
    //从下标[0]到指定下标[3],但不包括[3]
    NSString* str6 = [str substringToIndex:3];
    NSLog(@"%@ %@",str5,str6);
    
    
    
    //强转
    NSInteger i = [str4 intValue];
    NSLog(@"%d",i+1);
    double f = [str4 floatValue];
    long long l = [str4 longLongValue];
    
    
    
    //转大写
    NSString* bigStr = [str3 uppercaseString];
    NSLog(@"%@",bigStr);
    //转小写
    NSString* smallStr = [str2 lowercaseString];
    NSLog(@"%@",smallStr);
    //首字母大写
    NSString *firstPer = [str capitalizedString];
    
    
    
    
    //切割字符串
    NSArray *ary = [str componentsSeparatedByString:@","];
    
    
    
    //拼接字符串
    //stringByAppendingString
    NSString *appenStr = [str stringByAppendingString:@"aaa"];
    //appendString
    NSMutableString *mutableStr = [NSMutableString stringWithString:@"fsfsfsdff"];
    [mutableStr appendString:@"拼接的dfdsfdf"];
    
    
    
    
    //前后缀判断
    //判断前缀
    if ([str hasPrefix:@"44"])
    {
        NSLog(@"YES");
    }
    //判断后缀
    if ([str hasSuffix:@"0302"])
    {
        NSLog(@"YES");
    }
    //- (NSString *)commonPrefixWithString:(NSString *)str options:(NSStringCompareOptions)mask;
    //前缀忽略大小写,最终成功返回前缀"A",不成功返回 "空字符串"
    NSString *b5 = [@"A A" commonPrefixWithString:@"b" options:NSCaseInsensitiveSearch];
    
    
    
    //两字符串是否相等,完全匹配
    if ([str isEqualToString:str2])
    {
        NSLog(@"一样,");
        
    }
    
    
    
    
    //忽略大小写匹配两字符串
    NSComparisonResult b = [str2 caseInsensitiveCompare:str3];
    if (b==0) {
        NSLog(@"忽略大小写==0就是相等,-1不等");
    }
    b = [str2 compare:@"022"];
    
    
    
    
    
    //- (NSComparisonResult)compare:(NSString *)string
    //字符串高级匹配
    /*
     typedef NS_OPTIONS(NSUInteger, NSStringCompareOptions) {
     NSCaseInsensitiveSearch = 1,   //忽略大小写
     NSLiteralSearch = 2,   //区分大小写
     NSBackwardsSearch = 4,   //从源字符串的结尾搜索
     NSAnchoredSearch = 8,   //搜索限制范围的字符串
     NSNumericSearch = 64,   //按照字符串里的数字对比,必须相同格式如 1.1.1>1.1.0
     NSDiacriticInsensitiveSearch   //忽略 "-"符号的比较
     NSWidthInsensitiveSearch       //忽略字符串的长度,比较出结果
     NSForcedOrderingSearch         //忽略不区分大小写比较的选项,并强制返回 NSOrderedAscending或者 NSOrderedDescending
     NSRegularExpressionSearch      //只能应用于 rangeOfString:..., stringByReplacingOccurrencesOfString:...和 replaceOccurrencesOfString:... 方法。使用通用兼容的比较方法,如果设置此项,可以去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch
     */
    
    /*
     *  typedef NS_ENUM(NSInteger, NSComparisonResult)
     {NSOrderedAscending = -1L  升序
     NSOrderedSame,             相同
     NSOrderedDescending};      降序
     */
    NSString *numA = @"2.1.16";
    NSString *numB = @"2.1.11";
    
    NSComparisonResult bb = [numA compare:numB options:NSNumericSearch];
    if (bb == NSOrderedDescending)
    {
        NSLog(@"BigOne:%@",numA);
    }else
    {
        NSLog(@"smallOne:%@",numB);
    }
    
    
    
    //字符串比较 @"你4402042,0040302"
    NSComparisonResult b2 = [str localizedCompare:@"2042"];  //1 降序 -1升序  0相同
    
    
    
    //忽略大小写匹配
    NSComparisonResult b3 = [@"aa bb" localizedCaseInsensitiveCompare:@"AA BB"];  //1降序 -1升序  0相同
    
    
    
    //- (NSComparisonResult)localizedStandardCompare:(NSString *)string
    NSComparisonResult b4 = [@"A A" localizedStandardCompare:@"A A"];  //1 降序 -1升序  0相同
    
    
    
    //- (BOOL)containsString:(NSString *)str NS_AVAILABLE(10_10, 8_0);
    //字符串内容中是否包含指定项  ios8以上方法
    BOOL b6 = [str containsString:@"0040"];
    //- (BOOL)localizedCaseInsensitiveContainsString:(NSString *)str NS_AVAILABLE(10_10, 8_0);
    //忽略大小写判断包含字符串    ios8以上方法
    BOOL b7 = [@"adfdsffff" localizedCaseInsensitiveContainsString:@"FFFF"];
    
    
    
    //- (NSRange)rangeOfString:(NSString *)searchString;2.4
    //判断一个字符串是否包含另一个字符串:
    NSString *str11=@"中华人民共和国";
    NSString *str22=@"中华";
    NSString*str33= [str11 rangeOfString:str22].length !=0?@"found it":@"not found";
    NSLog(@"result:%@",str33);
    
    //根据所搜索字符串,返回所在位置,通常根据 range.lenght长度判断是否包含
    /* 对应可以设置 options:是否忽略大小写、数字对比....   range:限定范围处理
     - (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask;
     - (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)searchRange;
     - (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)searchRange locale:(nullable NSLocale *)locale
     */
    NSRange subRange = {4,4};
    NSRange range4 = [@"adfdsffff" rangeOfString:@"FFF" options:NSCaseInsensitiveSearch range:subRange];
    

    }
    //判断是否纯数字
    //第三种方式则是使用NSString的trimming方法

    • (BOOL)isPureNumandCharacters:(NSString *)string
      {
      string = [string stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
      if(string.length > 0)
      {
      return NO;
      }
      return YES;
      }

    相关文章

      网友评论

          本文标题:NSString (API+常用实例)

          本文链接:https://www.haomeiwen.com/subject/sefrsftx.html