NSString常用方法

2017-09-26  本文已影响0人  AntKing


- (void)test20 {
    //逆序
    NSString *temp = @"abcdefg";
    NSMutableArray *characters = @[].mutableCopy;
    for (int i = temp.length - 1; i >= 0; i--) {
        unichar c = [temp characterAtIndex:i];
        NSString *ch = [NSString stringWithCharacters:&c length:1];
        [characters addObject:ch];
    }
    
    NSString *result = [characters componentsJoinedByString:@""];
    NSLog(@"%@",result);
    for (NSString *ch in characters) {
        NSLog(@"%@",ch);
    }
}

- (void)test19 {
    NSString *temp = @"abcdefg";
    //转大写
    [temp uppercaseString];
    //转小写
    [temp lowercaseString];
}

- (void)test18 {
    
    //在string最后拼接字符或者字符串
    NSString *tempstr = @"/Users/lihe/Desktop/temp.";
    NSString *result = [tempstr stringByPaddingToLength:tempstr.length + 5  withString:@"plist" startingAtIndex:0];
    NSLog(@"%@",result);
}

//URL编码通常也被称为百分号编码(Url Encoding,also known as percent-encoding)

#pragma mark -----得到fullPath ---------------

- (void)test17 {
    //扩展文件路径
    NSString *tempstr = @"~user";
    NSString *result = [tempstr stringByExpandingTildeInPath];
    NSLog(@"%@",result);
}

#pragma mark -----字符串删除 ---------------

- (void)test16 {
    //删除后缀
    NSString *tempstr = @"/Users/lihe/Desktop/temp.plist";
    NSString *result = [tempstr stringByDeletingPathExtension];
    NSLog(@"%@",result);
}

- (void)test15 {
    //删除最后的文件名加后缀
    NSString *tempstr = @"/Users/lihe/Desktop/temp.plist";
    NSString *result = [tempstr stringByDeletingLastPathComponent];
    NSLog(@"%@",result);
}

#pragma mark -----数组拼接 ---------------
- (void)test14 {
    
    NSArray *stringArr = @[@"dd",@"ee",@"ff"];
    NSArray *sortedArr = [stringArr sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return  obj1 < obj2;
    }];
    
    NSString *joinString = [sortedArr componentsJoinedByString:@"-"];
    NSLog(@"%@",joinString);
}

#pragma mark -----比较字符串 ---------------
- (void)test13 {
    //字符串比较
    NSString *tempstr0 = @"aa";
    NSString *tempstr1 = @"bb";
    
    NSComparisonResult resut = [tempstr0 compare:tempstr1];
    
    if (resut == NSOrderedSame) {
        NSLog(@"相等");
    }else if(resut == NSOrderedAscending){
        NSLog(@"%@",@"升序");
    }else if (resut == NSOrderedDescending){
        NSLog(@"降序");
    }
}

#pragma mark -----字符串查找 ---------------

- (void)test12 {
    NSString *tempstr = @"aa-bb-cc";
    //当查找不到的时候,需要处理一下
    NSRange range = [tempstr rangeOfString:@"+"];
    if (range.length == 0) {
        NSLog(@"没找到");
        return;
    }
    NSLog(@"%@",NSStringFromRange(range));
    unichar c = [tempstr characterAtIndex:range.location];
    NSLog(@"%c",c);
}

- (void)test11 {
    NSString *tempstr = @"aa-bb-cc";
    NSString *subStr0 = [tempstr substringFromIndex:0];
    NSLog(@"%@",subStr0);
    
    NSString *subStr1 = [tempstr substringToIndex:tempstr.length - 1];
    NSLog(@"%@",subStr1);
    
    NSString *subStr2 = [tempstr substringWithRange:NSMakeRange(0, tempstr.length)];
    NSLog(@"%@",subStr2);
}

- (void)test10 {
    //字符串查找
    NSString *tempstr = @"aa-bb-cc";
    BOOL iscontain = [tempstr containsString:@"bb"];
    NSLog(@"%zd",iscontain);
}

#pragma mark -----字符串拼接---------------

- (void)test9 {
    //从数组中取元素拼接
    NSString *tempstr = @"aa-bb-cc";
    NSArray *stringArr = @[@"dd",@"ee",@"ff"];
    NSArray *append = [tempstr stringsByAppendingPaths:stringArr];
    NSLog(@"%@",append);
}

- (void)test8 {
    //格式化拼接
    NSString *tempstr = @"/Users/lihe/Desktop/temp";
    NSString *append = [tempstr stringByAppendingFormat:@"%zd",888];
    NSLog(@"%@",append);
}

- (void)test7 {
    //文件扩展
    NSString *tempstr = @"/Users/lihe/Desktop/temp";
    NSString *pathExtension = [tempstr stringByAppendingPathExtension:@".plist"];
    NSLog(@"%@",pathExtension);
}

- (void)test6 {
    //路径拼接
    NSString *tempstr = @"/Users/lihe/Desktop/";
    NSString *pahtComponet = [tempstr stringByAppendingPathComponent:@"niubi.plist"];
    NSLog(@"%@",pahtComponet);
}


- (void)test5 {
    NSString *tempstr = @"aa-bb-cc";
    NSString *append = [tempstr stringByAppendingString:@"gold is a girl"];
    NSLog(@"%@",append);
}


#pragma mark -----字符串替换 ---------------
- (void)test4 {
    //字符串替换
    NSString *tempstr = @"aa-bb-cc";
    NSRange range = [tempstr rangeOfString:@"bb" options:NSLiteralSearch];
    NSString *replace = [tempstr stringByReplacingCharactersInRange:range withString:@"oh"];
    NSLog(@"%@",replace);
}

- (void)test3 {
    //字符串替换
    NSString *tempstr = @"aa-bb-cc";
    NSRange range = NSMakeRange(0, tempstr.length);
    NSString *arr = [tempstr stringByReplacingOccurrencesOfString:@"-" withString:@"=" options:NSCaseInsensitiveSearch range:range];
    NSLog(@"%@",arr);
}

- (void)test2 {
    //字符串替换
    NSString *tempstr = @"aa-bb-cc";
    NSString *replacestr = [tempstr stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
    NSLog(@"%@",replacestr);
}


#pragma mark -----字符串切割 ---------------


- (void)test01 {
    //删除两端的字符
    NSString *tempstr = @"+aa-bb-cc-";
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"-+"];
    NSString *trim = [tempstr stringByTrimmingCharactersInSet:set];
    NSLog(@"%@",trim);
}

- (void)test1 {
    //按照characterSet中的字符来切割
    //NSCharacterSet有很多属性,空格键,control键,等
    NSString *tempstr = @"a,a-b,b,c,c-d";
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@",-"];
    NSArray *arr = [tempstr componentsSeparatedByCharactersInSet:set];
    NSLog(@"%@",arr);
}


- (void)test0 {
    //字符串切割
    NSString *tempstr = @"aa-bb-cc";
    NSArray *arr =  [tempstr componentsSeparatedByString:@"-"];
    NSLog(@"%@",arr);
}







上一篇 下一篇

猜你喜欢

热点阅读