NSString

作者: L柠_檬 | 来源:发表于2016-08-18 14:06 被阅读184次
    目录
    
        1.1 设置字符串行间距
        1.2 去除字符串前后的空格
        1.3 字符计算
        1.4 计算字符串高度
        1.5 null nil 转 @""
        1.6 分割字符串
        1.7 字符串比较大小
        1.8 查找字符串位置
        1.9 判断前后缀
        1.10 字符串提取
        1.12 保留小数点后两位
        1.13 用0补全
        1.14 %处理方法
        1.15 "处理方法
        1.16 获取图片类型
        1.17 字符串的每一字符
    
    
    1.1 设置字符串行间距
    
    .h
    
    + (NSMutableAttributedString *)stringWithSpacing:(NSString *)string 
                                         LineSpacing:(int)num;
    
    
    .m
    
    + (NSMutableAttributedString *)stringWithSpacing:(NSString *)string 
                                         LineSpacing:(int)num
    {
    //
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString 
    alloc] initWithString:string];
    
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle 
    alloc] init];
    
       [paragraphStyle setLineSpacing:num];//调整行间距
    
       [attributedString addAttribute:NSParagraphStyleAttributeName 
    value:paragraphStyle range:NSMakeRange(0, [string length])];
    
       return attributedString;
    
    }
    
    
    1.2 去除字符串前后的空格
    
    .h
    
    /**
    * 去除字符串前后的空格,判断输入的内容不是空格
    *
    * @param string 传入需要设置的字符串
    *
    * @return 去除空格后的字符串
    */
    
    .h
    
    + (NSString *)stringWithTrimming:(NSString *)string;
    
    .m
    
    + (NSString *)stringWithTrimming:(NSString *)string
    {
        //去除空格
         NSString *cleanString = [string stringByTrimmingCharactersInSet:
    [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
        return cleanString;
    
    }
    
    1.3 字符计算
    
    .h
    
    /**
    * 计算一个字符串相当于多少个字
    *
    * @param text 传入的字符串
    *
    * @return 字符个数
    */
    
    + (NSUInteger)stringWithLength:(NSString *)text;
    
    
    .m
    
    + (NSUInteger)stringWithLength:(NSString *)text
    {
    
        NSUInteger asciiLength = 0;
    
        for (NSUInteger i = 0; i < text.length; i++) {
    
             unichar uc = [text characterAtIndex: i];
             asciiLength += isascii(uc) ? 1 : 2;
        }
    
        NSUInteger unicodeLength = asciiLength / 2;
    
        if(asciiLength % 2) {
    
              unicodeLength++;
    
         }
         return unicodeLength;
    }
    
    1.4 计算字符串高度
    
    .h
    
    /**
    * 计算字符串高度
    *
    * @param string 需要计算的字符串
    * @param font label设置的字大小
    * @param width 需要计算内容的宽度
    *
    * @return CGSize类型
    */
    
    .h
    
    + (CGSize)stringWithHeight:(NSString *)string 
                      withFont:(UIFont *)font
                     withWidth:(CGFloat)width;
    
    .m
    
    + (CGSize)stringWithHeight:(NSString *)string 
                      withFont:(UIFont *)font 
                     withWidth:(CGFloat)width 
    {
    
        CGSize textBlockMinSize = CGSizeMake(width, 10000);
    
        CGSize size = CGSizeZero;
        NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine
                                       | NSStringDrawingUsesLineFragmentOrigin
                                       | NSStringDrawingUsesFontLeading;
    
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle 
      alloc] init];
    
        [paragraphStyle setLineSpacing:1];//调整行间距
    
        NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
    font,NSFontAttributeName,paragraphStyle,NSParagraphStyleAttributeName, nil];
    
        size = [string boundingRectWithSize:textBlockMinSize 
                                    options:options 
                                 attributes:attributes
                                    context:nil].size;
    
        return CGSizeMake(ceilf(size.width), ceilf(size.height));
    
    }
    
    
    1.5 null nil 转  @""
    
    .h
    
    + (NSString*)stringWithNull:(NSString *)string;
    
    .m
    
    // 转换空串
    +(NSString*)stringWithNull:(NSString *)string{
    
        if ([string isEqual:[NSNull null]]) {
    
            return @"";
    
        }else if ([string isKindOfClass:[NSNull class]]){
    
            return @"";
    
        }else if (string==nil){
    
            return @"";
    
        }
        return string;
    }
    
    
    
    1.6 分割字符串
    
    //用空格分割
    
        NSString *string=@"i am a person";
        
        NSArray *array=[string componentsSeparatedByString:@" "];
        
        
        //多符号切割
        
        NSString *str=@"1(333·)333 3333";
        
        NSArray *arr=[str componentsSeparatedByCharactersInSet:[NSCharacterSet 
                     characterSetWithCharactersInString:@"()· "]];
     
    
    1.7 字符串比较大小
    
    //字符串会一个字符一个字符比较,返回类型1 0 -1
    
    1 表示string1 大于 string2
    
    0 表示string1 等于 string2
    
    -1 表示string1 小于 string2
    
    
    
    例1:@"9"大于@"75"
    
    NSString *string1 = @"9";
    
    NSString *string2 = @"75";
    
    int result = [string1 compare:string2];
    
    NSLog(@"%d",result);//返回值是整形:1
    
    
    
    例2:@"72"小于@"75"
    
    NSString *string1 = @"72";
    
    NSString *string2 = @"75";
    
    int result = [string1 compare:string2];
    
    NSLog(@"%d",result);//返回值是整形:-1
    
    
    
    例3:@"8"等于@"8"
    
    NSString *string1 = @"8";
    
    NSString *string2 = @"8";
    
    int result = [string1 compare:string2];
    
    NSLog(@"%d",result);//返回值是整形:0
    
    
    
    例4:不区分大小写比较
    
    NSString *string1 = @"a";
    
    NSString *string2 = @"A";
    
    int result = [string1 compare:string2 options:NSCaseInsensitiveSearch]; 
    
    NSLog(@"%d",result); //返回值是整形:0
    
    1.8 查找字符串位置
    
    NSString *str = @"lemon";
    
    NSRange range = [str rangeOfString:@"em"];
    
    NSLog(@"location = %lu, length = %lu",range.location,range.length);
    
    1.9 判断前后缀
    
    //1.前缀
    NSString *str = @"http://www.baidu.com";
    
    BOOL isHave = [str hasPrefix:@"http://"];
    
    NSLog(@"%d",isHave);
    
    
    //2.后缀
    NSString *str = @"http://www.baidu.com";
    
    BOOL isHave = [str hasSuffix:@"com"];
    
    NSLog(@"%d",isHave);
    
    
    1.10 字符串提取
    
    //例1:从0开始,到第5个
    NSString *str = @"http://www.baidu.com";
    
    NSString *topStr = [str substringToIndex:5];
    
    NSLog(@"%@",topStr); 
    
    http:
    
    
    //例2:从第5个到最后一个
    NSString *str = @"http://www.baidu.com";
    
    NSString *endStr = [str substringFromIndex:5];
    
    NSLog(@"%@",endStr);
    
    //www.baidu.com
    
    
    //例3:从第2个到第7个
    NSString *str = @"http://www.baidu.com";
    
    NSString *rangeStr = [str substringWithRange:NSMakeRange(2, 7)];
    
    NSLog(@"%@",rangeStr);
    
    tp://ww
    
    
    1.11 UTF8转码
    
    NSString* str = [str stringByAddingPercentEscapesUsingEncoding:
    NSUTF8StringEncoding];
    
    
    1.12 保留小数点后两位
    
    NSString *string = [NSString stringWithFormat:@"%.2f",M_PI];
    
    //输出结果是: 3.14
    
    NSLog(@"%@", string);
    
    1.13 用0补全
    
    NSInteger count = 5;
    
    //02代表:如果count不足2位 用0在最前面补全(2代表总输出的个数)
    
    NSString *string = [NSString stringWithFormat:@"%02zd",count];
    
    //输出结果是: 05
    
    NSLog(@"%@", string);
    
    1.14 %处理方法
    
    NSInteger count = 50;
    
    //%是一个特殊符号 如果在NSString中用到%需要如下写法
    NSString *string = [NSString stringWithFormat:@"%zd%%",count];
    
    //输出结果是: 50%
    NSLog(@"%@", string);
    
    1.15 "处理方法
    
    NSInteger count = 50;
    
    //"是一个特殊符号, 如果在NSString中用到"需要用\进行转义
    
    NSString *string = [NSString stringWithFormat:@"%zd\"",count];
    
    //输出结果是: 50"
    
    NSLog(@"%@", string);
    
    1.16 获取图片类型
    
    - (NSString *)contentTypeForImageData:(NSData *)data {
    
         uint8_t c;
    
         [data getBytes:&c length:1];
    
        switch (c) {
    
            case 0xFF:
    
            return @"jpeg";
    
            case 0x89:
    
            return @"png";
    
            case 0x47:
    
            return @"gif";
    
            case 0x49:
    
            case 0x4D:
    
            return @"tiff";
    
            case 0x52:
    
            if ([data length] < 12) {
    
                return nil;
    
            }
    
            NSString *testString = [[NSString alloc] initWithData:
            [data subdataWithRange:NSMakeRange(0, 12)] 
            encoding:NSASCIIStringEncoding];
    
            if ([testString hasPrefix:@"RIFF"] && [testString 
          hasSuffix:@"WEBP"]) 
            {
                  return @"webp";
            }
    
            return nil;
    
        }
    
        return nil;
    
    }
    
    1.17 获取字符串的每一字符
    
    - (NSArray *)words:(NSString *)string;
    {
    
    #if ! __has_feature(objc_arc)
    
    NSMutableArray *words = [[[NSMutableArray alloc] init] autorelease];
    
    #else
    
    NSMutableArray *words = [[NSMutableArray alloc] init];
    
    #endif
    
        const char *str = [string       cStringUsingEncoding:NSUTF8StringEncoding];
    
        char *word;
    
        for (int i = 0; i < strlen(str);) {
    
            int len = 0;
    
            if (str[i] >= 0xFFFFFFFC) {
    
                len = 6;
    
            } else if (str[i] >= 0xFFFFFFF8) {
    
               len = 5;
    
            } else if (str[i] >= 0xFFFFFFF0) {
    
              len = 4;
    
            } else if (str[i] >= 0xFFFFFFE0) {
    
              len = 3;
    
            } else if (str[i] >= 0xFFFFFFC0) {
    
              len = 2;
    
            } else if (str[i] >= 0x00) {
    
                len = 1;
    
            }
    
            word = malloc(sizeof(char) * (len + 1));
    
            for (int j = 0; j < len; j++) {
    
                word[j] = str[j + i];
    
            }
    
            word[len] = '\0';
    
            i = i + len;
    
            NSString *oneWord = [NSString stringWithCString:word           encoding:NSUTF8StringEncoding];
    
             free(word);
    
            [words addObject:oneWord];
    
            }
    
        return words;
    
    }
    

    相关文章

      网友评论

          本文标题:NSString

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