• 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 的常见用法====================

  • 相关阅读:
    C#读写XML no
    ComboBox、ListBox绑定和获取数据 no
    win7 安装oracle 11g no
    2011没有惊天动地 no
    Oracle 删除重复数据只留一条 no
    Oracle 创建用户并分配权限 no
    Oracle 导出、导入某用户所有数据(包括表、视图、存储过程...) no
    谢谢,博客园这么快就审批通过了我的申请!
    Eclipse 中 JAVA AWT相关包不提示问题(解决)
    Eclipse java项目转Maven项目
  • 原文地址:https://www.cnblogs.com/calence/p/5274153.html
Copyright © 2020-2023  润新知