首页 > 代码库 > Object-c 之 字符串

Object-c 之 字符串

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        NSString *s1;
        s1=[NSString stringWithFormat:@"%d + %d = %d",2,3,2+3];
        NSLog(@"%@",s1);
        NSLog(@"length:%ld",[s1 length]);
        
        NSString *s2=@"2 + 3 = 5";
        if([s1 isEqualToString:s2]){
            NSLog(@"s1 isEqualToString:s2");
        }else{
            NSLog(@"s1 isNotEqualToString:s2");
        }
        
        if(s1 == s2){
            NSLog(@"s1 == s2");
        }else{
            NSLog(@"s1 != s2");
        }
        
        if([s1 compare:s2]){
            NSLog(@"s1 compare s2");
        }else{
            NSLog(@"s1 not compare s2");
        }
        
        
        //拼接字符串
        NSString *s3 =[NSString stringWithFormat:@"%@%@",s1,s2];
        NSLog(@"%@",s3);
        
        s3=@"";
        s3=[s1 stringByAppendingString:s2];
        NSLog(@"%@",s3);

    }
    return 0;
}


运行结果


Object-c 之 字符串