美文网首页
OC字符串基本使用

OC字符串基本使用

作者: super_2e20 | 来源:发表于2017-12-11 20:20 被阅读0次

    一.不可变字符串的方法

    创建字符串

      NSString *str0 = [[NSString alloc]init];
      //简单创建字符传
      NSString *str1 = @"skdjfksjdklfs";
      //对象方法创建字符传
      NSString *str2 = [[NSString alloc]initWithString:str1];
      //通过C语言中字符串创建OC语言中字符串
      char strc[20] = "this is the test ";
      NSString *str3 = [[NSString alloc]initWithUTF8String:str];
      //通过类创建字符串
      NSString *str4 = [NSString stringWithString:str1];
      NSString *str5 = [NSString stringWithUTF8String:strc];
      NSString * str6 = [NSString stringWithFormat:@"%@",str1];
    

    从本地文件获取字符串


    NSString *path  = @"Users/File";
    NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:/*&error*/nil];
    

    从网络中获取

      NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
      NSString *string = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    

    字符串拼接

     NSString  *str1  =  @"skjdhjkshfkdsss";
     NSString *str2 = [str1  stringByAppendingString :@"!"];
    

    字符串长度

     NSString *str = @"ghjkl;ksjhdfsidhfs";
     unsigned long len = [str length];
    

    取其中一个字符串

    char c  = [str1 characterAtIndex: 2];
    

    比较连个字符串大小

    NSString *str1 = @"skjdhjkshfkdsss"; NSString *str2 = @"skjdhjkshfksdlfjsfdsss"; NSComparisonResult result = [str1 compare:str2];

      if (result == NSOrderedAscending) {
    
        NSLog(@"str1 < str2");
    
      } else if (result== NSOrderedDescending){
        NSLog(@"str1 > str2");
    
      }else{
        NSLog(@"str1 = str2");
    
      }
    

    大小写转换

    NSString *string = @"jsjsdjk  sdhfjksj  sjdf";
    NSString *str = [string lowercaseString];
    NSString *str = [string uppercaseString];
    NSSting *str = [string capitalizedString];
    

    查找字符串

    NSRange range = [string  rangeOfString:@"jsdjk"];
    if (range.location == NSNotFound){
    
     //没有找到
    
    } else {
    
    }
    if(range.length == 0){
    
    //没有找到 如果找到了 length 就是要查找字符串的长度
    
    }
    //从右往左查
    NSRange rangeFront = [string rangOfString:@"sdk"option:NSBackwardsSearch];
    
    //判断前后缀
    Bool isS = [str hasSuffix];
    Bool isP = [str hasPrefix];
    
    //将字符串转成数字
    int num = [@"123" intValue];
    
    //提取字符串 确定起始位置
      NSString *str = [@"skjdo lskdl lsl" substringFromIndex:2];
    
    //确定结束位置
       NSString *str = [string6 substringToIndex:5];
    
    //确定其实下标和结束位置(长度)
      NSString *str = [@"dskj" substringWithRange:NSMakeRange(1, 2)];
    

    二.可变字符串的方法

     //创建空字符串
      NSMutableString *muStr = [[NSMutableString alloc]init];
    
      //capacity 表示的是一个大致范围
      NSMutableString *str = [[NSMutableString alloc]initWithCapacity:10];
    
      //可变字符串不能用此种方法创建 如果使用了 就是不可变字符串
      NSString *string = @"sdjksd";
    
      //通过对象方法创建可变字符传
      NSMutableString *str1 = [[NSMutableString alloc]initWithString:@"yes"];
      NSMutableString *str3 = [[NSMutableString alloc] initWithFormat:@"%@",str];
    
      //通过类方法创建可变字符串
      NSMutableString *muStr21 = [NSMutableString stringWithString:@"sds"];
      NSMutableString *mutStr33 = [NSMutableString stringWithFormat:@"%@",string];
    
     //通过本地文件
      NSMutableString *mut223 = [NSMutableString stringWithContentsOfURL:@"/Users/01-字符串/File"  encoding:NSUTF8StringEncoding error:nil];
    
      //通过网络
        NSMutableString *mutStr9 = [NSMutableString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.baidu.com"] encoding:NSUTF8StringEncoding error:nil];
    
    可变字符串增 字符串
      NSMutableString *mut = [NSMutableString stringWithString:@"sjhdj sihfsdiu"];
      [mut1 appendString:@"sdkjf"];
      [mut1 appendFormat:@"dsfsds"];
    
      //在任意位置添加
      [mut1 insertString:@"sdfs" atIndex:2];
    
      //删除任意位置字符串
      NSMutableString mut = [NSMutableString stringWithString:@"sdkjfsdk"];
      [mut deleteCharactersInRange:NSMakeRange(8, 8)];
    
        //修改字符串
      NSMutableString *mut5 = [NSMutableString stringWithString:@"sd sjd skjdf"];
    
      //直接用后面的字符串替代现有的字符串
      [mut5 setString:@"sdkfsf"];
    
      //用存在的字符串替换选中位置的字符串
      [mut5 replaceCharactersInRange:NSMakeRange(2, 2) withString:@"sdsf"];
    
      //查
      NSMutableString *mut6 = [NSMutableString stringWithString:@"sun is a girl "];
      for (int i= 0; i< [mut6 length]; i++) {
        char s = [mut6 characterAtIndex:i];
      }
    
    将字符串funnel与字符串also之间的字符串提取出来,不可以数
    NSString *string = @"NSString primitive (funnel) methods. A minimal subclass of NSString just needs to implement these, although we also recommend";
    NSRange range1 = [string rangeOfString:@"funnel"];
    NSRange range2 = [string rangeOfString:@"also"];
    NSRange range = NSMakeRange(range1.location + range1.length, range2.location - range1.location - range1.length);
    NSString *subString = [string substringWithRange:range];
    NSLog(@"*%@*", subString);
    

    相关文章

      网友评论

          本文标题:OC字符串基本使用

          本文链接:https://www.haomeiwen.com/subject/bvfwixtx.html