1223.1——字符串有关操作

用一段程序来说明:


#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int age = 20;
        int height = 170;
        
        /*NSString
        //1.静态 alloc new copy mutablecopy
        NSString *str = @"hello world";
        //2.alloc 空字符串对象
        NSString *str2 = [[[NSString alloc] init] autorelease];
        //3.alloc 有初始值
        NSString *str3 = [[[NSString alloc] initWithString:str] autorelease];
        //不要自己控制内存 工厂方法 便利器方法
        NSString *str31 = [NSString stringWithString:str];
        NSLog(@"%@", str31);
        
        //4.字符串按照一定的格式拼接 age = 20 height = 170
        NSString *str4 = [[[NSString alloc] initWithFormat:@"age=%d height=%d", age, height] autorelease];
         
        NSString *str4 = [NSString stringWithFormat:@"age=%d height=%d", age, height];
        NSLog(@"%@", str4);
        */
        NSString *str = @"虽然书生不强硬霸权,但是偶尔出手,却也已经让他在这个仁义飞扬,激昂热情的仙侠世界里边名声飞扬了。";
        //1. 获取字符串的长度 length
        NSUInteger length = str.length;
        NSLog(@"lengh of str is %ld", length);
        
        //2.获取子字符串
        //substringFromIndex index->end
        NSString *sStr = [str substringFromIndex:4];
        NSLog(@"fromIndex:4 %@", sStr);
        
        //substringToIndex  start->index
        NSString *toStr = [str substringToIndex:4];
        NSLog(@"toIndex:4 %@", toStr);
        
        //substringWithRange
        /*NSRange range = {4, 8};
          NSRange range;
          range.location = 4;
          range.length = 8;
         */
        NSRange range = NSMakeRange(4, 8);
        NSString *rangeStr = [str substringWithRange:range];
        NSLog(@"range(4,8) %@", rangeStr);
        
        //3.字符串的比较
        //比较两个字符串是否相同 isEqualToString (YES NO)
        if ([@"hello" isEqualToString:@"hello"]) {
            NSLog(@"the same");
        } else{
            NSLog(@"not the same");
        }
        
        /*compare
         NSOrderedAscending a < b
         NSOrderedSame a == b
         NSOrderedDecending a > b
         */
        NSComparisonResult result = [@"ABC" compare:@"BC"];
        switch (result) {
            case NSOrderedAscending:
                NSLog(@"ABC < BC");
                break;
            case NSOrderedSame:
                NSLog(@"ABC = BC");
                break;
            case NSOrderedDescending:
                NSLog(@"ABC > BC");
                break;
            default:
                break;
        }
        
        
        //4. 字符换的查找
        //http(s)://www.baidu.com   .jpg  .mp3  .mov
        NSString *url = @"http://pic33.nipic.com/20131008/13661616_190558208000_2.jpg";
        
        //判断某个字符串是否以http开头 前缀
        if ([url hasPrefix:@"http"]) {
            NSLog(@"url");
        } else{
            NSLog(@"invalid");
        }
        
        if ([url hasSuffix:@"jpg"]) {
            NSLog(@"picture");
        }else{
            NSLog(@"not jpg picture");
        }
        
        //获取子字符串的位置
        NSRange rg = [url rangeOfString:@"pic33"];
        if (rg.length != 0){
            NSLog(@"location:%ld length:%ld", rg.location, rg.length);
        } else{
            NSLog(@"Not fond");
        }
        
        
        //5. 字符串->数字
        NSString *priceStr = @"33.5";
        float price = [priceStr floatValue];
        NSLog(@"price: %f", price);
        
        int temp = 20;
        NSString *tempStr = [NSString stringWithFormat:@"this costs $%d", temp];
        
        //NSMutableString
        //预留一片空间 效率更高
        //NSMutableString *mstr2 = [NSMutableString stringWithCapacity:0];

        NSMutableString *mStr = [[NSMutableString alloc] initWithCapacity:0];
        
        int num = 4;
        //appendString
        [mStr appendString:@"四大名著"];
        [mStr appendFormat:@"%d 大名著", num];
        [mStr insertString:@"《" atIndex:0];
        [mStr insertString:@"》" atIndex:mStr.length];
        NSLog(@"%@", mStr);
        
        //字符串的替换
        NSRange rge = [mStr rangeOfString:@"四大名著"];
        [mStr replaceCharactersInRange:rge withString:@"三国演义"];
        NSLog(@"%@", mStr);
        
        //删除子字符串
        NSRange yge = [mStr rangeOfString:@"演义"];
        [mStr deleteCharactersInRange:yge];
        NSLog(@"%@", mStr);
        
        //重新设置一个字符串
        [mStr setString:@"西游记"];
        NSLog(@"%@", mStr);
        
        
        
    }
    return 0;
}

原文地址:https://www.cnblogs.com/damonWq/p/5070999.html