iOS基础学习

NSString各种简单的操作

2017-10-23  本文已影响4人  WenJim
// 用来保存错误信息
NSError *error = nil;

// 读取文件内容
NSString *str = [NSString stringWithContentsOfFile:@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt" encoding:NSUTF8StringEncoding error:&error];

// 如果有错误信息
if (error) {
    NSLog(@"读取失败, 错误原因是:%@", [error localizedDescription]);
} else { // 如果没有错误信息
    NSLog(@"读取成功, 文件内容是:\n%@", str);
}
NSString *str = @"ZBC 爱してる";
BOOL flag = [str writeToFile:@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (flag == 1)
{
    NSLog(@"写入成功");
}else{
    NSLog(@"写入失败");
}
NSString *str1 = @"ZBC 爱してる";
BOOL flag = [str1 writeToFile:@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];

NSString *str2 = @"ZBC 사랑해요";
[str2 writeToFile:@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];

NSString *str = [NSString stringWithContentsOfFile:@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt" encoding:NSUTF8StringEncoding error:&error];
NSLog(@"str = %@", str);

输出结果:ZBC 사랑해요
NSString * str1 = @"abc";
// 转化为大写字母
    NSString * newStr = [str1 uppercaseString];
    NSLog(@"newStr = %@",newStr);
    
    NSString * str1 = @"ABC";
    // 转化为小写字母
    NSString * newStr2 = [str1 lowercaseString];
    NSLog(@"newStr2 = %@",newStr2);
    
NSString * str1 = @"abc";
    // 首字母大写,其他字母小写
    NSString * newStr3 = [str1 capitalizedString];
    NSLog(@"newStr3 = %@",newStr3);
 NSString *str1 = @"cwj";
    NSString *str2 = [NSString stringWithFormat:@"cwj"];
    if ([str1 isEqualToString:str2]) {
        NSLog(@"字符串内容一样");
    }

    if (str1 == str2) {
        NSLog(@"字符串地址一样");
    }
    // NSOrderedAscending   前面的小于后面的
    // NSOrderedSame        两个字符串相等
    // NSOrderedDescending  前面的大于后面的
    switch ([str1 compare:str2]) {
        case NSOrderedAscending:
        {
            NSLog(@"str1 小于 str2");
        }
            break;
        case NSOrderedSame:
        {
            NSLog(@"str1 等于 str2");
        }
            break;
        case NSOrderedDescending:
        {
            NSLog(@"str1 大于 str2");
        }
            break;
            
        default:
            break;
    switch ([str1 caseInsensitiveCompare:str2]) {
        case NSOrderedAscending:
        {
            NSLog(@"str1 小于 str2");
        }
            break;
        case NSOrderedSame:
        {
            NSLog(@"str1 等于 str2");
        }
            break;
        case NSOrderedDescending:
        {
            NSLog(@"str1 大于 str2");
        }
            break;
            
    }
    // 本质就是从字符串的第一个字符开始匹配,只要不匹配就返回NO
    if ([str hasPrefix:@"http://"]) {
        NSLog(@"是一个URL");
    }else{
        NSLog(@"不是一个URL");
    }
    if ([str hasSuffix:@".png"]) {
        NSLog(@"图片");
    }else{
        NSLog(@"不是图片");
    }
    NSString * str = @"Hello,yulin.";
    // 只要str中包含该字符串,那么就会返回该字符串在str 中的起始位置以及该字符串的长度
    // location 从0开始,length 从1开始
    // 如果str 中没有需要查找的字符串,那么返回的range的length = 0,location = NSNotFound
    NSRange range = [str rangeOfString:@"yulin"];
//    NSLog(@"location = %lu, length = %lu",range.location,range.length);
    
    if (range.location == NSNotFound) {
        NSLog(@"str中没有需要查找的字符串");
    }else{
        NSLog(@"str中有需要查找的字符串");
        NSLog(@"location = %lu, length = %lu",range.location,range.length);
    }

字符串截取

NSString * str = @"<head>ZBC 爱してる</head>";
    str = [str substringFromIndex:7];
    NSLog(@"str = %@", str);

输出结果: ZBC 爱してる</head>
NSString *str = @"<head>ZBC 爱してる</head>";
    str = [str substringToIndex:10];
    NSLog(@"str = %@", str);

输出结果: <head>ZBC 爱してる
NSString *str = @"<head>ZBC 爱してる</head>";
    NSRange range;
    /*
    range.location = 6;
    range.length = 8;
    */
    range.location = [str rangeOfString:@">"].location + 1;
    range.length = [str rangeOfString:@"</"].location - range.location;
    NSString *res = [str substringWithRange:range];
    NSLog(@"res = %@", res);
输出结果: ZBC 爱してる

字符串的替换函数

NSString * str = @"http:&&www.xiaoningle.com&img&logo.png";
    // OccurrencesOfString:要替换谁
    // withString:用谁替换
    NSString * newStr =  [str stringByReplacingOccurrencesOfString:@"&" withString:@"/"];
    NSLog(@"newStr = %@",newStr);
NSString *str =  @"   http://www.xiaoningle.com/img/logo.png   ";
    NSString *newStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSLog(@"str =|%@|", str);
    NSLog(@"newStr =|%@|", newStr);

输出结果:
str =|   http://www.xiaoningle.com/img/logo.png   |
newStr =|http://www.xiaoningle.com/img/logo.png|
    NSString * str = @"HTTP://www.xiaoningle.com/img/LOGO.PNG";
    NSCharacterSet * set = [NSCharacterSet uppercaseLetterCharacterSet];
    NSString * newStr = [str stringByTrimmingCharactersInSet:set];
    
    NSLog(@"newStr = %@",newStr);
输出结果:
newStr =://www.xiaoningle.com/img/LOGO.

NSString与路径

 // 其实就是判断是否以/开头
//    NSString *str = @"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt";
    NSString *str = @"Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt";
    if ([str isAbsolutePath]) {
        NSLog(@"是绝对路径");
    }else
    {
        NSLog(@"不是绝对路径");
    }
// 截取最后一个/后面的内容
   NSString * str =@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt";
    NSString *component = [str lastPathComponent];
    NSLog(@"component = %@", component);
 NSString * str =@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt";
    // 本质就是删除最后一个/ 后面的内容,包括/ 也会被删除
    NSString * newStr2 = [str stringByDeletingLastPathComponent];
    NSLog(@"newStr2 = %@",newStr2);
NSString * str =@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt";
// 本质就是在字符串的末尾加上一个/ 和指定的内容
    // 注意: 如果路径后面已经有了/ ,那么就不会添加了
    //       如果路径后面有多个/ ,那么会自动删除多余的/,只保留一个
    NSString * newStr3 = [str stringByAppendingPathComponent:@"zbc"];
    NSLog(@"newStr3 = %@",newStr3);
    // 本质就是从字符串的末尾开始查找. ,截取第一个.后面的内容
NSString * str =@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt";
    NSString * newStr5 = [str pathExtension];
    NSLog(@"newStr5 = %@",newStr5);
    // 本质就是从字符串的末尾开始查找. ,删除第一个.和.后面的内容
NSString * str =@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt";
    NSString * newStr6 = [str stringByDeletingPathExtension];
    NSLog(@"newStr6 = %@",newStr6);
    // 本质就是在字符串的末尾加上一个. 和指定的内容
NSString * str =@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt";
    NSString * newStr7 = [str stringByAppendingPathExtension:@"png"];
    NSLog(@"newStr7 = %@",newStr7);

字符串与基本数据类型转换

NSString * str = @"120";
NSLog(@"length = %lu",str.length);
NSString *str1 = @"110";
    NSString *str2 = @"10";
    int res = str1.intValue + str2.intValue;
    NSLog(@"res = %i", res);

NSString *str3 = @"110";
    NSString *str4 = @"10.1";
    double res2 = str3.doubleValue + str4.doubleValue;
    NSLog(@"res2 = %f", res2);
    char * cStr = "cwj";
    NSString * str5 = [NSString stringWithUTF8String:cStr];
    NSLog(@"str5 = %@",str5);
    
    NSString * str6 = @"cwjLove";
    const char * cStr2 = [str6 UTF8String];
    NSLog(@"cStr2 = %s",cStr2);
上一篇下一篇

猜你喜欢

热点阅读