NSString常见用法总结

//====================NSStirng 的常见用法====================

-(void)testString

{

    //创建格式化字符串:占位符(由一个%加一个字符组成)

    NSString* str1 = [NSString stringWithFormat:@"the first string!"];

    NSLog(@"one:::%@",str1);

    

    NSString* str2 = [[NSString alloc]initWithString:@"the second string!"];

    NSLog(@"two:::%@",str2);

    

    //从文件中读取字符串

    NSString* path = [[NSBundle mainBundle]pathForResource:@"strings" ofType:@"txt"];

//    NSDictionary* contents = [[NSDictionary alloc]initWithContentsOfFile:path];

//    NSArray* keys = [contents allKeys];

    NSString* asstring = [[NSString alloc]initWithContentsOfFile:path];

    NSLog(@"three:::%@",asstring);

    

    //比较两个字符串

    NSString* str3 = @"This is a String!";

    NSString* str4 = @"This is a String! tyutuy";

    BOOL result = [str3 isEqualToString:str4];

    NSLog(@"the result is %d",result);

    

    //输出大小写字母

    NSLog(@"upper 大写字母:%@",[str3 uppercaseString]);//大写

    

    //调用appendFormat追加字符串

    NSMutableString* muString = [[NSMutableString alloc]initWithString:@"this is a string!"];

    [muString appendFormat:@"ending is end!"];

    NSLog(@"the result is %@",muString);

    

    //调用appendString方法追加字符串

    [muString appendString:@"good life is coming!"];

    NSLog(@"%@",muString);

    

    //在指定位置插入字符串

    [muString insertString:@"iiiiiii  " atIndex:0];

    NSLog(@"%@",muString);

    

    [muString setString:@"sajfk;sadjflsajfkls"];

    NSLog(@"%@",muString);

    

    

    //用指定字符串替换字符串中某指定位置、长度的字符串

    [muString replaceCharactersInRange:NSMakeRange(0, 2) withString:@"==="];

    NSLog(@"%@",muString);

    

    

    //文件扩展名

    NSString* pathString = @"~/记录.txt";

    NSLog(@"Extension:%@",[pathString pathExtension]);

}

//====================NSStirng 的常见用法====================

posted @ 2016-03-13 23:13  bky2016  阅读(399)  评论(0编辑  收藏  举报