NSString方法亲测

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str,*str1,*str2;
        NSError *error;
        str = [[NSString alloc] initWithContentsOfFile:@"/Users/apple/qishuaipeng/1.txt" encoding:NSUTF8StringEncoding error:&error];//从文件初始化字符串
        NSLog(@"%@",str);
        NSURL *url = [NSURL URLWithString:@"http://blog.csdn.net/jiuchabaikaishui/article/details/39989219"];//NSURL其实就是我们在浏览器上看到的网站地址
        str1 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];//用网址初始化字符串
        NSLog(@"%@",str1);
        NSURL *url1 = [NSURL URLWithString:@"http://www.sina.com.cn"];
        str2 = [[NSString alloc] initWithContentsOfURL:url1 encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000) error:nil];//GB2312编码的转化
        NSLog(@"%@",str2);
        NSLog(@"%@",[NSString stringWithFormat:@"12345678"]);
        NSString *str3 = [NSString stringWithCString:"abc" encoding:NSASCIIStringEncoding];//c语言字符串转化为字符串对象
        NSLog(@"%@",str3);
        const char *cstr = [str3 cStringUsingEncoding:NSUTF8StringEncoding];//字符串对象转化为c语言字符串
        NSLog(@"%s",cstr);
        NSString *str4 = @"Hello world!";//创建字符串对象,用常量赋值初始化
        NSLog(@"%@",str4);
        NSString *str5 = [[NSString alloc] initWithFormat:@"10.1234ajdls:d%d---%.2f++**%@",10,10.f,str4];//格式化初始化字符串对象
        NSLog(@"%@",str5);
        const char *utf8 = [str5 UTF8String];
        NSLog(@"%s",utf8);
        NSLog(@"%.4f",str5.floatValue);//取出字符串对象前面的浮点型数据
        CGPoint point = CGPointMake(10, 20);
        NSString *str6 = NSStringFromPoint(point);//用点初始化字符串
        NSLog(@"%@",str6);
        NSString *str7 = [str5 uppercaseString];//字符串字母全部大写
        NSLog(@"%@",str7);
        NSString *str8 = [str5 lowercaseString];//字符串字母全部小写
        NSLog(@"%@",str8);
        NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"ABCabc"];
        NSString *str9 = [str8 stringByTrimmingCharactersInSet:set];
        NSLog(@"---%@",str9);
        NSString *str10 = [str9 stringByPaddingToLength:10 withString:@"abc" startingAtIndex:2];//
        NSLog(@"+++%@",str10);
        NSString *str11 = @"sftp://class.room:22";
        if ([str11 hasPrefix:@"sftp://"]) {//是否有某个前缀
            NSLog(@"这是自己定义的协议。");
        }
        if ([str11 hasSuffix:@"22"]) {//是否有某个后缀
            NSLog(@"22号端口。");
        }
        if ([str11 contaiansString:@"class"]) {//是否包含某个字符串
            NSLog(@"包含class!");
        }
        NSString *str12 = [str11 substringFromIndex:7];//返回从某个位置开始的子字符串,左开右闭
        NSLog(@"%@",str12);
        NSString *str13 = [str11 substringToIndex:10];//返回到某个位置结束的子字符串
        NSLog(@"%@",str13);
        NSString *str14 = [str11 substringWithRange:NSMakeRange(2, 9)];//返回某个范围的子字符串
        NSLog(@"%@",str14);
        if (NSOrderedAscending == [@"abc"compare:@"ABC"]) {//字符串比较
            NSLog(@"小于");
        }
        if (NSOrderedDescending == [@"abc"compare:@"ABC"]) {
            NSLog(@"大于");
        }
        //应用安装
        NSString *home = NSHomeDirectory();
        NSLog(@"%@",home);
        NSLog(@"%@",[home stringByAppendingString:@"/Document"]);
        NSLog(@"%@",[home componentsSeparatedByString:@"/"]);//以某个子字符串切割成数组
        NSString *path = [home stringByAppendingString:@"/Documents/test.txt"];
        [home writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值