美文网首页
iOS 关于NSString的一些基本用法

iOS 关于NSString的一些基本用法

作者: 绚雨蓝了个枫 | 来源:发表于2017-07-27 15:19 被阅读230次

    1、rangeOfString: 查找字符串某处是否包含其它字符串。
    如果存在则替换: stringByReplacingOccurrencesOfString: withString: replacement options: range:

       NSString *string11 = @"Stay,hungry,Stay,foolish";
        
       NSString *string12 = @"hungry";
        
       NSRange range = [string11 rangeOfString:string12];
        
         if (range.location == NSNotFound || range.length == 0) {
            
               NSLog(@"不包含string12");
            
          }else{
            
               string11 = [string11 stringByReplacingOccurrencesOfString:string12 withString:@"there" options:NSCaseInsensitiveSearch range:range];
          }
    
       NSLog(@"string11--->>>%@",string11);
    
       打印结果: string11--->>>Stay,there,Stay,foolish
    
    

    2、substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符。
    substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符。

        NSString *string21 = @"Stay,hungry,Stay,foolish";
        
        NSString *string22 = [string21 substringToIndex:4];
        
        NSLog(@"string22--->>>%@",string22);
        
        打印结果: string22--->>>Stay
        ```
    
    NSString *string23 = @"Stay,hungry,Stay,foolish";
    
    NSString *string24 = [string23 substringFromIndex:5];
    
    NSLog(@"string24--->>>%@",string24);
    
    打印结果: string24--->>>hungry,Stay,foolish
    
    ```
    

    3、substringWithRange: 按照所给出的位置,长度,任意地从字符串中截取子串

    NSString *string31 = @"Stay,hungry,Stay,foolish";
    
    NSString *string32 = [string31 substringWithRange:NSMakeRange(0, 4)];
    
    NSLog(@"string32--->>>%@",string32);
    
    打印结果: string32--->>>Stay
    

    4、replaceCharactersInRange: withString: 用指定字符串替换字符串中某指定位置、长度的字符串

        NSMutableString *String41 = [[NSMutableString alloc] initWithString:@"Stay,hungry,Stay,foolish"];
        
        [String41 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"Very"];
        
        NSLog(@"String41--->>>%@",String41);
        
        打印结果:  String41--->>>Very,hungry,Stay,foolish
    

    5、hasPrefix: 检查字符串是否以另一个字符串开头

        NSString *String51 = @"Stayhungry.txt";
        
        [String51 hasPrefix:@"Stay"] == 1 ?  NSLog(@"hasPrefix--->>>YES") : NSLog(@"hasPrefix--->>>NO");
        
        打印结果:  hasPrefix--->>>YES
        
        [String51 hasSuffix:@".txt"] == 1 ?  NSLog(@"hasSuffix--->>>YES") : NSLog(@"hasSuffix--->>>NO");
        
        打印结果:  hasSuffix--->>>YES
        ```
      
    6、扩展路径
    
    
    NSString *Path = @"~/hungry.txt";
    
    NSString *absolutePath = [Path stringByExpandingTildeInPath];
    
    NSLog(@"absolutePath--->>>%@",absolutePath);
    ```
    

    打印结果如图所示:


    absolutePath.png
        NSLog(@"Path--->>>%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
        
        打印结果: Path--->>>~/hungry.txt
        ```
       7、获取文件扩展名
    
    NSString *newPath = @"~/hungry.txt";
    NSLog(@"Extension--->>>%@",[newPath pathExtension]);
    
    打印结果: Extension--->>>txt
    ```
    

    8、用C比较:strcmp函数

    //    strcmp函数是比较两个字符串的大小,返回比较的结果。一般形式是:
    //    i = strcmp(字符串1,字符串2);
    //    其中,字符串1、字符串2均可为字符串常量或变量;i   是用于存放比较结果的整型变量。比较结果是这样规定的:
    //    ①字符串1小于字符串2,strcmp函数返回一个负值;
    //    ②字符串1等于字符串2,strcmp函数返回零;
    //    ③字符串1大于字符串2,strcmp函数返回一个正值;
        
        char string1[] = "hungry!";
        
        char string2[] = "hungry!";
        
        if(strcmp(string1, string2) == 0)
        {
            NSLog(@"strcmp函数--->>>返回零");
        }
         打印结果:strcmp函数--->>>返回零
        ```
        
       9、compare方法(comparer返回的三种值)
    
    
    //NSOrderedSame
    NSString *astring91 = @"Esay,foolish!";
    
    NSString *astring92 = @"Esay,foolish!";
    
    BOOL result912 = [astring91 compare:astring92] == NSOrderedSame;    //NSOrderedSame判断两者内容是否相同
    
    NSLog(@"result912--->>>%d",result912);
    
    打印结果:  result912--->>>1
    
    //NSOrderedAscending
    NSString *astring93 = @"Esay,foolish!";
    
    NSString *astring94 = @"esay,foolish!";
    
    BOOL result934 = [astring93 compare:astring94] == NSOrderedAscending;    //NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,astring93小于astring94为真) NSOrderedAscending的意思是:左边的操作对象小于右边的对象。--->升序
    
    NSLog(@"result934--->>>%d",result934);
    
    打印结果: result934--->>>1
    
    //NSOrderedDescending
    NSString *astring95 = @"esay,foolish!";
    
    NSString *astring96 = @"Esay,foolish!";
    
    BOOL result956 = [astring95 compare:astring96] == NSOrderedDescending;    //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring95大于astring96为真) NSOrderedDescending的意思是:左边的操作对象大于右边的对象。--->降序
    
    NSLog(@"result956--->>>%d",result956);
    
    打印结果:result956--->>>1
    
        
    10、不考虑大小写比较字符串
    
        //caseInsensitiveCompare
        NSString *astring101 = @"esay,foolish!";
        
        NSString *astring102 = @"Esay,foolish!";
        
        BOOL result1012 = [astring101 caseInsensitiveCompare:astring102] == NSOrderedSame;    //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring101大于astring102为真)
        
        NSLog(@"result1012--->>>%d",result1012);
        
    打印结果: result1012--->>>1
        
        //compare: options:
        NSString *astring103 = @"esay,foolish!";
        
        NSString *astring104 = @"Esay,foolish!";
        
        BOOL result1034 = [astring103 compare:astring104
                                 options:NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame;    //NSCaseInsensitiveSearch:不区分大小写比较。 NSNumericSearch:按照字符串里的数字为依据,算出顺序。
        
        NSLog(@"result1034--->>>%d",result1034);
    
    打印结果:   result1034--->>>1
        
        
        //NSStringCompareOptions的枚举类型
        NSArray *stringsArray = [NSArray arrayWithObjects:
                                 @"hungry 2",
                                 @"Hungry 1",
                                 @"Hungry 300",
                                 @"Hungry 4000",
                                 @"hungry 05000",nil];
        
        NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch |NSNumericSearch |NSWidthInsensitiveSearch |NSForcedOrderingSearch;
        //NSWidthInsensitiveSearch:忽略字符串的长度,比较出结果。
        //NSForcedOrderingSearch:忽略不区分大小写比较的选项,并强制返回 NSOrderedAscending 或者 NSOrderedDescending
        
        NSLocale *currentLocale = [NSLocale currentLocale];//当前用户设置的本地化对象
        
        NSComparator finderSortBlock = ^(id string1,id string2) {
            NSRange string1Range =NSMakeRange(0, [string1 length]);
            return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale];
        };
        
        NSArray *finderSortArray = [stringsArray sortedArrayUsingComparator:finderSortBlock];
        
        NSLog(@"finderSortArray--->>>%@", finderSortArray);
        
    打印结果:
        finderSortArray--->>>(
                              Hungry 1,
                              hungry 2,
                              Hungry 300,
                              Hungry 4000,
                              hungry 05000,
                              )
    
        
     11、输出大写或者小写字符串
    
    
    NSString *string111 = @"hungry";
    
    NSString *string112 = @"Hungry";
    
    NSString *string113 = @"stay,hungry";
    
    NSLog(@"string111--->>>%@",[string111 uppercaseString]);//大写
    
    打印结果: string111--->>>HUNGRY
    
    NSLog(@"string112--->>>%@",[string112 lowercaseString]);//小写
    
    打印结果: string112--->>>hungry
    
    NSLog(@"string113--->>>%@",[string113 capitalizedString]);//英文首字母大写,中文字符被忽略,不会报错
    
    打印结果: string113--->>>Stay,Hungry
    

    相关文章

      网友评论

          本文标题:iOS 关于NSString的一些基本用法

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