首页 > 其他 > 详细

NSString基础-创建、长度、比较、判断大小、转换

时间:2016-01-06 15:57:28      阅读:175      评论:0      收藏:0      [点我收藏+]
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //创建字符串常量
        NSString *str1=@"char1";
        //实例方法创建
        NSString *str2=[[NSString alloc]init];
        NSString *str4=[[NSString alloc]initWithFormat:@"char4"];
        NSString *str5=[[NSString alloc]initWithString:str1];
        //类方法创建
        NSString *str6=[NSString string];
        NSString *str7=[NSString stringWithString:str1];
        NSString *str8=[NSString stringWithFormat:@"char8"];
        //格式化创建
        int num1=3;
        float num2=4.5;
        NSString *str9=[[NSString alloc]initWithFormat:@"%f",num2];
        NSString *str10=[[NSString alloc]initWithFormat:@"%d,%f",num1,num2];
        NSString *str11=[NSString stringWithFormat:@"%d,%f",num1,num2];
        
        //字符串长度
        NSUInteger len=[str1 length];
        
        //字符串比较
        if([str7 isEqualToString:str8]){
            NSLog(@"They are sam");
        }
        else{
            NSLog(@"They are different");
        }
        
        //判断字符串大小
        NSComparisonResult result1=[str7 compare:str8];
        NSComparisonResult result2=[str7 caseInsensitiveCompare:str8];
        
        //字符串转换
        float num3=[str9 floatValue];
        
        //大小写转换
        NSString *str12=[[NSString alloc]initWithFormat:@"HelloWorld!"];
        NSString *str13=[str12 uppercaseString];
        NSString *str14=[str12 lowercaseString];
        NSString *str15=[str12 capitalizedString];
        
        //将NSString转换为C语言字符串
        const char * str16=[str12 UTF8String];
    }
    return 0;
}

 

NSString基础-创建、长度、比较、判断大小、转换

原文:http://www.cnblogs.com/wangyang1987/p/5105706.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!