OC_NSString

//

//  main.m

//  OC_NSString

//

//  Created by qianfeng on 15/6/10.

//  Copyright (c) 2015年 qianfeng. All rights reserved.

//



#import <Foundation/Foundation.h>



//NSString 字符串类 创建不可变字符串对象的类

//NSMutableString 创建可变字符串对象类



//OC语言完全兼容C语言,C语言中的函数,以及语法都可以在OC中使用

//OC字符串与C语言字符串区别

//1.OC字符串是一个字符串对象,字符串常量需要用 @"" 包含

//2.C语言字符串用 "" 包含

//3.C语言的字符串存储的是以字符的ASCII形式存储

//4.OC中的字符串存储是以uicode编码(万国码)形式存储

//UTF-8(多字节编码)

//5.打印OC的字符串用 %@ ,打印C语言的字符串用 %s;

//

//

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        NSString *str1 = @"hello world";//@"hello world"是一个字符串常量,存储在常量区

        NSLog(@"str1 = %@",str1);

        //格式化创建字符串对象

        //对象方法

        //- (instancetype)initWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);

        NSString *str2 = [[NSString alloc] initWithFormat:@"%s%d%@","hello",123,@"world"];

        NSLog(@"str2 = %@",str2);

        //格式化的类方法创建字符对象

        //+ (instancetype)stringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);

        

        NSString *str3 = [NSString stringWithFormat:@"%s%d%@","qian",456,@"feng"];

        NSLog(@"str3 = %@",str3);

        

        //用给定的字符串对象创建字符串对象

        NSString *str4 = @"中国教育";

        NSString *str5 = [[NSString alloc] initWithString:str4];

        NSLog(@"str5 = %@",str5);

        

        //用C的字符串创建OC的字符串对象

        NSString *str6 = [[NSString alloc] initWithUTF8String:"qianfengjiaoyu"];

        NSLog(@"str6 = %@",str6);

        

        NSString *str7 = [[NSString alloc] initWithCString:"千锋qianfeng" encoding:NSUTF8StringEncoding];

        NSLog(@"str7 = %@",str7);

        

        //创建一个空的字符串对象 @"" ;

        NSString *str8 = [NSString string];

        NSLog(@"str8 = %@",str8);

        

        NSString *str9 = [[NSString alloc] init];

        NSLog(@"str9 = %@",str9);

        

        //跟initWithString相对应

        NSString *str10 = [NSString stringWithString:str7];

        NSLog(@"str10 = %@",str10);

        

        //跟initWithUTF8String相对应

        NSString *str11 = [NSString stringWithUTF8String:"hello world 中国"];

        NSLog(@"str11 = %@",str11);

        

        

        NSString *str12 = [NSString stringWithCString:"zhongguo" encoding:NSUTF8StringEncoding];

        NSLog(@"str12 = %@",str12);

    }

    return 0;

}

 

你可能感兴趣的:(NSString)