Objective-C之字符串NSString

浏览: 64 发布日期: 2016-10-10 分类: objective-c

创建字符串

NSString *aStr = @"I am a String";
NSString *cStr = @"I am a String";

用标准c创建字符串

char *Cstring = "我是字符串";  
NSString *astring = [[NSString alloc] initWithUTF8String:Cstring];  
NSLog(@"astring:%@",astring);

字符串的内存地址

NSLog(@"astr point adress: 0x%.8x",aStr);
NSLog(@"astr point adress: 0x%.8x",bStr);
bStr = @"I am a String";
NSLog(@"%@",bStr);
NSLog(@"astr point adress: 0x%.8x",aStr);
NSLog(@"astr point adress: 0x%.8x",bStr);

2016-10-09 18:00:40.802217 NSStringDemo[13717:1142243] astr point adress: 0x00002060
2016-10-09 18:00:40.802360 NSStringDemo[13717:1142243] astr point adress: 0xb7b48920
2016-10-09 18:00:40.802499 NSStringDemo[13717:1142243] I am a String
2016-10-09 18:00:40.802512 NSStringDemo[13717:1142243] astr point adress: 0x00002060
2016-10-09 18:00:40.802521 NSStringDemo[13717:1142243] astr point adress: 0x00002060

格式化字符串

//格式化输出符号
%@     对象  
%d, %i 整数  
%u     无符整形  
%f     浮点/双字  
%x, %X 二进制整数  
%o     八进制整数  
%zu    size_t  
%p     指针  
%e     浮点/双字 (科学计算)  
%g     浮点/双字  
%s     C 字符串  
%.*s   Pascal字符串  
%c     字符  
%C     unichar  
%lld   64位长整数(long long)  
%llu   无符64位长整数  
%Lf    64位双字  
%e 是实数,用科学计数法计的  

字符串比较

void compare() {
    bool result = [aStr isEqualToString:cStr];
    if (result) {
        NSLog(@"两个字符串相同");
    } else {
        NSLog(@"两个字符串不同");

    }
}

2016-10-09 18:00:12.548501 NSStringDemo[13701:1141412] 两个字符串相同

字符串分割

void split() {
    NSString *str = @"美国,中国,日本,俄罗斯,英国";
    //字符串数组指针
    NSArray *array = [str componentsSeparatedByString:@","];
    for (int i = 0; i < [array count]; i++) {
        NSLog(@"string: %@",[array objectAtIndex:i]);
    }
}

2016-10-09 18:00:40.802763 NSStringDemo[13717:1142243] string: 美国
2016-10-09 18:00:40.802796 NSStringDemo[13717:1142243] string: 中国
2016-10-09 18:00:40.802806 NSStringDemo[13717:1142243] string: 日本
2016-10-09 18:00:40.802815 NSStringDemo[13717:1142243] string: 俄罗斯
2016-10-09 18:00:40.802823 NSStringDemo[13717:1142243] string: 英国

字符串匹配

void match() {
    NSString *str = @"美国,中国,日本,俄罗斯,英国";

    //前缀
    if ([str hasPrefix:@"美国"]) {
        NSLog(@"字符串以美国开头");
    }

    //后缀
    if ([str hasSuffix:@"英国"]) {
        NSLog(@"字符串以英国结尾");
    }
}

2016-10-09 18:00:40.802835 NSStringDemo[13717:1142243] 字符串以美国开头
2016-10-09 18:00:40.802862 NSStringDemo[13717:1142243] 字符串以英国结尾

字符串搜索和替换

void searchAndReplace() {
    NSString *string = @"我们是 gong产 主义 接班人";
    NSString *temp = @"gong产";
    //字符串位置
    NSRange range1 = [string rangeOfString:temp];
    NSLog(@"temp在字符串中的起始位置: %lu.",(unsigned long)range1.location);
    NSLog(@"temp在字符串中的结束位置: %lu.",(unsigned long)(range1.location + range1.length));

    //替换字符串
    NSString *str = [string stringByReplacingCharactersInRange:range1 withString:@"共产"];
    NSLog(@"%@",str);

    str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"%@",str);

}

2016-10-09 18:00:40.802877 NSStringDemo[13717:1142243] temp在字符串中的起始位置: 4.
2016-10-09 18:00:40.802888 NSStringDemo[13717:1142243] temp在字符串中的结束位置: 9.
2016-10-09 18:00:40.802959 NSStringDemo[13717:1142243] 我们是 共产 主义 接班人
2016-10-09 18:00:40.803017 NSStringDemo[13717:1142243] 我们是共产主义接班人
2016-10-09 18:00:40.803073 NSStringDemo[13717:1142243] 姓名: 刘国权, 年龄: 27岁
2016-10-09 18:00:40.803108 NSStringDemo[13717:1142243] 姓名: liuguoquan, 年龄: 27岁
2016-10-09 18:00:40.803126 NSStringDemo[13717:1142243] 姓名: liuguoquan, 年龄: 29岁
返回顶部