ios 中NSString的一些调用


#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //创建
        NSString *str1 = @"abc";
        NSLog(@"%p", str1);
//        str1 = [[NSString alloc] initWithString:@"abc"];
//        NSLog(@"%p", str1);
        
        //1. 将C语言字符串转化为OC的字符串对象,只能是UTF-8编码
        str1 = [[NSString alloc] initWithUTF8String:"abc"];
        //2. 支持更多编码
        str1 = [[NSString alloc] initWithCString:"abc" encoding:NSUTF8StringEncoding];
        int money = 10;//我有10块钱
        //3. 格式化字符, NSLog()
        str1 = [[NSString alloc] initWithFormat:@"我有%d块钱", money];
        
        //4. 类方法
        str1 = [NSString stringWithUTF8String:"abc"];
        //5. 所有以名词开头的都会有返回值
        str1 = [NSString stringWithFormat:@"我有%d块钱", money];
        
        NSError *error;
        //6. 将文本文件内容读取到字符串对象中,并将首地址保存到str1里
        str1 = [NSString stringWithContentsOfFile:@"/Users/apple/Desktop/test.txt" encoding:NSUTF8StringEncoding error: &error];
        if (error != nil) {
            NSLog(@"%@", error);
        }
        else {
            NSLog(@"%@", str1);
        }
        
        str1 = @"ab";
        //7. 如果文件不存在,创建并保存内容;如果存在的话,覆盖
        [str1 writeToFile:@"/Users/apple/Desktop/test2.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
        
        //8. 将基本数据类型(C类型)转化为OC的对象
        str1 = [NSString stringWithFormat:@"%d", 123];
        NSLog(@"%@", str1);
        int value = str1.intValue;
        NSLog(@"%d", value);
        
        //9. 比较
//        @"abc", @"abd"
//        NSComparisonResult res = [@"abc" compare:@"abd"];
        NSString *path = @"www.baidu.com";
        //10. 检查前缀
        if (![path hasPrefix:@"http://"]) {
            path = [NSString stringWithFormat:@"http://%@", path];
        }
        NSLog(@"%@", path);
        
        //11. 检查后缀
        NSString *filename = @"1.jpg";
        if ([filename hasSuffix:@".jpg"]) {
            NSLog(@"这是一张jpg图片");
        }
        
        //12. 在一个字符串后添加一个字符串, 产生一个新的字符串,老的不变
        NSString *str2 = [str1 stringByAppendingString:@"1111"];
        NSLog(@"%@", str1);
        NSLog(@"%@", str2);
        
        str2 = [str1 stringByAppendingFormat:@"%d", 998];
        NSLog(@"%@", str2);
        
        NSString *desk = @"/Users/apple/Desktop";
        for (int i = 1; i < 11; i++) {
            NSLog(@"%@", [desk stringByAppendingFormat:@"/%d.txt", i]);
        }
        
        //13. 文件路径,自动添加分隔符
        str2 = [desk stringByAppendingPathComponent:@"1.txt"];
        NSLog(@"%@", str2);
        str2 = [desk stringByAppendingPathExtension:@"exe"];
        NSLog(@"%@", str2);
        
        NSArray *array = [str2 pathComponents];
        NSLog(@"%@", [array lastObject]);
        
        filename = [array lastObject];
        //删除扩展名
        filename = [filename stringByDeletingPathExtension];
        NSLog(@"filename: %@", filename);
        
        //CSV
        NSString *values = @"1,2;3";
//        array = [values componentsSeparatedByString:@",;"];
        
        //用@",;"里的字符生成一个字符的集合
        NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@",;"];
        //使用字符集set中的字符分割字符串
        array = [values componentsSeparatedByCharactersInSet:set];
        NSLog(@"%@", array);
        
        str1 = @"def教育abc";
        NSLog(@"字符个数: %lu", str1.length);
        
        NSLog(@"字节长度: %lu", [str1 lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
        
        unichar c = [str1 characterAtIndex:0];
        NSLog(@"%hu", c);
        
//        const char *cstr = [str1 UTF8String];
//        NSLog(@"cstr: %s", cstr);
        
        str1 = @"abcdefghijk";
        //将字符串扩充到10个字节,如果超过10个,裁剪
        str1 = [str1 stringByPaddingToLength:10 withString:@".+-" startingAtIndex:2];
        NSLog(@"%@", str1);
        
        str1 = @"abcd  zhang  san   aaaabcabc";
        //裁剪首尾的字符
        str1 = [str1 stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"zd abc"]];
        NSLog(@"|%@|", str1);
        
        str1 = @"01234567890";
        //获取一个字串[3, 11)
        str2 = [str1 substringFromIndex:3];
        NSLog(@"%@", str2);
        
        //[0, 3)
        str2 = [str1 substringToIndex:3];
        NSLog(@"%@", str2);
        
        //截取一个范围内的字串
        NSRange range = NSMakeRange(0, 5);//{0, 3};
        str2 = [str1 substringWithRange:range];
        NSLog(@"%@", str2);
        
        str1 = @"world hello world";
        //查找一个字串的位置, 默认为从左到右的第一个
        range = [str1 rangeOfString:@"world"];
        NSLog(@"{%lu, %lu}", range.location, range.length);
        //没有找到,返回NSNotFound
        if (range.location == NSNotFound) {
            NSLog(@"没找到");
        }
        
        //将字符串中出现的world全部替换为hi
        str2 = [str1 stringByReplacingOccurrencesOfString:@"world" withString:@"hi"];
        NSLog(@"%@", str2);
        
        str1 = @"hi, China";
        //大写
        str2 = str1.uppercaseString;
        NSLog(@"%@", str2);
        
        //小写
        str1 = str2.lowercaseString;
        NSLog(@"%@", str1);
        
        //首字母大写
        str2 = str1.capitalizedString;
        NSLog(@"%@", str2);
        
        NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
        NSString *content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%@", content);
        [content writeToFile:@"/Users/apple/Desktop/index.html" atomically:YES encoding:NSUTF8StringEncoding error:nil];
        
        str1 = @“教育";
        //百分号转义
        str2 = [str1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"%@", str2);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/daijiahong/p/4151938.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值