OC字符串简单使用

#import <Foundation/Foundation.h>
#import "MyFirstClass.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str = @"123456789";
        NSLog(@"%@",[str substringFromIndex:3]);    ///fromIndex:从xx开始
        NSLog(@"%@",[str substringToIndex:6]);      ///ToIndex:截止xx
        NSLog(@"%@",[str substringWithRange:NSMakeRange(3, 4)]);///NSRange NSMakeRange(NSUInteger loc, NSUInteger len) :从xx开始,长度xxx
        NSLog(@"%@", str);  ///NSString是不可变的
        NSMutableString *mutableString;//NSMutableString是可变的
        mutableString = [NSMutableString stringWithString: @"lalalalal"];   //NSMutableString初始化不能用字面量
        [mutableString insertString:@"fuck" atIndex:[mutableString length]]; //在index插入
        NSLog(@"%@", mutableString);
        NSLog(@"%d",[mutableString isEqualToString: str]);//字符串比较
        [mutableString setString: @"fuckyou"]; //重置NSMutableString
        NSLog(@"%@", mutableString);
        [mutableString deleteCharactersInRange: NSMakeRange(1, 2)];//删除对应range的子串
        NSLog(@"%@", mutableString);
        [mutableString replaceCharactersInRange:NSMakeRange(1, 2) withString: @"damn"];//用withString来替换range部分的子串
        NSLog(@"%@", mutableString);
    }
    return 0;
}

 

posted @ 2020-11-25 15:10  NeoZy  阅读(130)  评论(0编辑  收藏  举报