美文网首页
iOS 字符串

iOS 字符串

作者: 清风_____ | 来源:发表于2020-10-29 16:59 被阅读0次

    1、字符串的截取

    //1.截取字符串
    NSString *string =@"123456d890";
    NSString *str1 = [string substringToIndex:5];//截取掉下标5之前的字符串
    NSLog(@"截取的值为:%@",str1);
     NSString *str2 = [string substringFromIndex:3];//截取掉下标3之后的字符串
    NSLog(@"截取的值为:%@",str2);
    

    2、匹配字符串

    从字符串(sd是sfsfsAdfsdf)中查找(匹配)字符串(Ad)

    //2.匹配字符串
    NSString *string =@"sd是sfsfsAdfsdf";
    NSRange range = [string rangeOfString:@"Ad"];//匹配得到的下标
    NSLog(@"rang:%@",NSStringFromRange(range));
    string = [string substringWithRange:range];//截取范围内的字符串
    NSLog(@"截取的值为:%@",string);
    

    3、字符串分割

    根据字符串中的某个字符(A)来分割字符串

    //3.分隔字符串
    NSString *string =@"sdfsfsfsAdfsdf";
     NSArray *array = [string componentsSeparatedByString:@"A"]; //从字符A中分隔成2个元素的数组
    NSLog(@"array:%@",array); //结果是adfsfsfs和dfsdf
    

    4、NSString的反转

    如abcdefg反转后为gfedcba

    NSString * str = @"beijing huan ying nin";
    unsigned long len;
    len = [str length];
    unichar a[len];
    for(int i = 0; i < len; i++)
    {
    unichar c = [str characterAtIndex:len-i-1];
    a[i] = c;
    }
    NSString *str1=[NSString stringWithCharacters:a length:len];
    NSLog(@"--- 反转之后的字符串 ----%@",str1);
    

    5、数组拼接成字符串

    NSString *string = [array componentsJoinedByString:@","];//--分隔符
    

    6、汉字转拼音,小写转大写,大写转小写

    NSString *tempStr = @"你好";
    //转成可变字符串 
    NSMutableString *tempStr1 = [NSMutableString stringWithString:tempStr];
    //先转换为带声调的拼音
    CFStringTransform((CFMutableStringRef)tempStr1, NULL, kCFStringTransformToLatin, false);
    //再转换为不带声调的拼音 
    CFStringTransform((CFMutableStringRef)tempStr1, NULL, kCFStringTransformStripDiacritics, false);
    NSLog(@"汉字转拼音:tempStr1 == %@", tempStr1);  // ni hao
    tempStr1 = (NSMutableString *)tempStr1.uppercaseString;
    NSLog(@"小写转大写:tempStr1 == %@", tempStr1);  // NI HAO
    tempStr1 = (NSMutableString *)tempStr1.lowercaseString;
    NSLog(@"大写转小写:tempStr1 == %@", tempStr1);  // ni hao
    

    7、字符串类型判断

    #define ENGLISH @"[a-zA-Z]"  // 英文
    #define CHINESE @"[\u4e00-\u9fa5]"  // 汉字
    #define INTSTR @"[0-9]"  // 数字
    #define SMALLLETTER @"[a-z]"  // 小写字母
    #define CAPITALLETTER @"[A-Z]"  // 大写字母
    
    /*!
    * @brief 字符串判断
    * @param str 字符串,strRegex 正则表达式
    * @return 返回YES或NO
    */
    - (BOOL)typeJudgeWithStr:(NSString *) str withType:(NSString *) strRegex
    {
        NSPredicate *strPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", strRegex];
        return [strPredicate evaluateWithObject:str];
    }
    

    8、字典转JSON字符串,JSON字符串转字典

    /*!
    * @brief 把格式化的JSON格式的字符串转换成字典
    * @param jsonString JSON格式的字符串
    * @return 返回字典
    */
    + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
      return nil;
    } 
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
    options:NSJSONReadingMutableContainers
    error:&err];
    if(err) {
    NSLog(@"json解析失败:%@",err);
    return nil;
    }
    return dic;
    }
    // 字典转json字符串
    + (NSString*)dictionaryToJson:(NSDictionary *)dic
    {
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    

    9、字符串(大、小写)

    // 把字符串全部转化为小写
    NSString  *str =@"AAaaBBbb";
    NSString * str = [str lowercaseString];
    
    // 把字符串全部转化为大写
    NSString * str = [str uppercaseString];
    
    //开头大写,其余小写
    NSString * str =[str capitalizedString];
    

    链接:https://www.jianshu.com/p/d17885464698
    链接:https://www.cnblogs.com/somebodywx/p/5724892.html
    链接:https://www.jianshu.com/p/c8702df7d6ce
    链接:https://www.jianshu.com/p/5b418818dbce

    相关文章

      网友评论

          本文标题:iOS 字符串

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