• 【iOS开发】-NSString的扩展使用


    第一:基本数据类型与字符串转换

    //基本数据类型(int float,double char)

    1)基本数据类型—>NSString       
    //1.int类型换换成字符串
    int a = 88;
    NSString * str1 = [NSString stringWithFormat:@"%d",a];
           
    //2.float -> NSString
    float f = 0.8;
    NSString * str3 = [NSString stringWithFormat:@"%.1f",f];
    
    //3.double - >NSstring
     double d = 66.2222;
     NSString * str4 = [NSString stringWithFormat:@"%.4f",d];
           
    //4.char - > NSString
    char c = ‘b';
    NSString * str5 = [NSString stringWithFormat:@"%c",c];
           
    2)NSString-—>基本数据类型     
    
    //5.NSString -> int
     NSString * str6 = @"1";
    int i1 = [str6 intValue];
           
    //5.NSString -> float
    NSString * str7 = @“8.8";
    float f1 = [str7 floatValue];
           
    //6.NSString -> double
    NSString * str8 = @"8.88";
    double d1 = [str8 doubleValue];
           
    //注意:当你要把字符串转换成某种基本数据类型的时候,一定要确定你要转化你的数据是正确
    NSString * str9 = @“a123";
    int i2 = [str9 intValue];
           
    //7.将字符串中的字母转换成大写
     NSString * str10 = @“apple";
    NSString * tmpStr1 = [str10 uppercaseString];
    NSLog(@"tmpStr1 %@ str10 %@",tmpStr1,str10);
           
    //8.将字符中的字母转换成小写
    NSString * str11 = @“APPLE";
    NSString * tmpStr2 = [str11 lowercaseString];
    
    //9.将首字母大写
    NSString * str12 = @“apple";
    NSString * tmpStr3 = [str12 capitalizedString];
    

    第二:字符串重组

    //1.多个数据拼接成一个字符串
    int year = 1990;
    int month = 8;
    int day = 08;
    NSString * date = [NSString stringWithFormat:@"%d年%d月%d日",year,month,day];
    
    NSString * address = @“上海";
    NSString * dateAndAddrss = [NSString stringWithFormat:@"%@地址:%@",date,address];
    
    //2.字符串的末尾追加新的字符
    NSString * str1 = @"apple.";
    NSString * str2 = [str1 stringByAppendingString:@"iphone"];
    
    //3.在指定的范围追加字符串
    NSString * str3 = @"apple";
    NSRange range = {4,0};
    //location代表的时从哪个索引开始插入,length 代表的意思可以覆盖几个字符
    NSString * str4 = [str3 stringByReplacingCharactersInRange:range withString:@"iphone"];
    
    //4.使用新的字符,替换原有的字符
    NSString * str5 = @“apple,iPhone";
    NSString * str6 = [str5 stringByReplacingOccurrencesOfString:@“p" withString:@“q"];
    
    //5.在字符串中删除特定的某些字符 胡汉三 hhs
    NSString * str7 = @“apple-iphone—.iwatch.itouch";
    NSString * str8 = [str7 stringByReplacingOccurrencesOfString:@"-" withString:@""];
    
    //6.去掉字符串中的空格
    NSString * str9 = @“apple iPhone iwatch";
    NSString * str10 = [str9 stringByReplacingOccurrencesOfString:@" " withString:@""];
    

    第三: 字符串写入文件

      3.1:将字符串写入到文件
    NSString * content = @"今天马路上捡到一分钱";
    NSString * path = @"/Users/tengfei/Desktop/test.txt";
    //1.NSString *:要给入文件目录(并且要包括文件名称)
    //2.atomically : YES
    //3.NSUTF8StringEncoding 国际通用的编码格式
    //4.NSError 是用来记录错误信息的
    NSError * error;
    //writeToFile:<#(NSString *)#> atomically:<#(BOOL)#> encoding:<#(NSStringEncoding)#> error:<#(NSError *__autoreleasing *)#>
    BOOL result = [content writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
    
    if(result)
    {
        NSLog(@"文件创建成功");
    }
    else
    {
        NSLog(@"error %@",error);
    }
    
      3.2:从文件中读取字符串内容
      //要给出文件路径
      //你写入文件的时候,使用的是什么编码格式,读取的时候就要用什么编码格式
    NSString * path = @"/Users/tengfei/Desktop/test.txt";
    NSError * error;
    NSString * str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    
    if (error != nil)
    {
        NSLog(@"error message %@",error);
    }
    else
    {
        NSLog(@"str content %@",str);
    }
    

    第四:可变字符串

    //1.创建可变字符串
    //NSMutableString 继承自 NSString
    //NSMutableString 拥有NSString所有的属性以及方法
    //NSMutableString * muStr = [[NSMutableString alloc] init];
    NSMutableString * muStr = [[NSMutableString alloc] initWithCapacity:0];
    
    //是初始化字符串内容
    [muStr setString:@"apple"];
    
    //动态的给字符串末尾追加新值
    [muStr appendString:@".com"];
    
    //在指定的索引位置,插入字符串
    [muStr insertString:@“iphone" atIndex:3];
    
    //删除指定范围的字符串
    //你要删除的长度,一定不能够超出字符串的的长度,导致异常Range or index out of bounds
    NSRange range = {3,7};
    // [muStr deleteCharactersInRange:range];
    
    [muStr replaceCharactersInRange:range withString:@"苹果"];
  • 相关阅读:
    Mybatis学习(2)原始dao开发和使用mapper接口代理开发
    Mybatis学习(1)
    Leetcode | Merge Intervals
    GDB打印STL容器内容
    LeetCode | Max Points on a Line
    最长不减子序列【转】
    LeetCode | Evaluate Reverse Polish Notation
    LeetCode | Word Ladder II
    LeetCode | Valid Number
    LeetCode | Set Matrix Zeroes
  • 原文地址:https://www.cnblogs.com/fengtengfei/p/4316338.html
Copyright © 2020-2023  润新知