NSString常用方法

//  main.m
//  20150407-NSString
#import <Foundation/Foundation.h>

BOOL isEqual(int a, int b) {
    
//    return a - b;//错误写法
//    BOOL a == 1 的时候才是YES,其他情况全是NO;
//    所以在比较的时候,不要用a == YES这种方式;
    if (a == b) {
        return YES;
    }
    return NO;
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
#pragma mark - BOOL类型介绍
//        四种bool类型,下来了解后面两种类型
//        BOOL yesOrNo;
//        bool trueOrFalse;
//        Boolean;
//        boolean_t;
        
//        BOOL a = 3;//BOOL 只有1为YES 0为NO
//        NSLog(@"%d", a == YES);//BOOL 类型中 YES = 1;NO = 0;a = 3时,a == YES为假
//        NSLog(@"%d", isEqual(5, 5));
//        bool b = 3;
//        NSLog(@"%d", b == true);//bool 非零即是1
        
#pragma mark - NSString介绍
//        常量字符串
        NSString *string = @"This-is-a-const-string";//直接初始化一个常量字符串(记忆此方法)
//        init初始化
//        根据string来初始化,直接用init来初始化是没用的,因为常量字符串初始化以后不能修改
        NSString *string1 = [[NSString alloc] initWithString:string];
        NSString *string2 = [[NSString alloc] initWithFormat:@"%d%f", 2, 5.3];//可变参数 格式化:将数字2转换成字符串
        NSLog(@"%@", string1);
        NSLog(@"%@", string2);
//        类方法:便利构造
        NSString *string3 = [NSString stringWithString:string2];//NSString的方法都是以string开头的
        NSString *string4 = [NSString stringWithFormat:@"hello world!%@", string1];//开发过程中用的最多的方法,可添加占位符(记忆此方法)
        NSLog(@"%@", string3);
        NSLog(@"%@", string4);
        
//        拼接
        NSString *string5 = [string stringByAppendingString:string2];//对象方法:对象一 stringByAppendingString 对象二
        NSString *string6 = [NSString stringWithFormat:@"%@%@", string, string2];//类方法:也可用这种方式拼接
        NSLog(@"%@", string5);
        NSLog(@"%@", string6);
//        截取
        NSString *string7 = [string1 substringFromIndex:3];//截取从第4个字符到最后一个字符
        NSString *string8 = [string1 substringToIndex:9];//截取从第一个字符开始的连续9个字符
        NSRange range = NSMakeRange(2, 15);//NSMakeRange(开始位置,长度)//NSrang不是一个对象
        NSString *string9 = [string1 substringWithRange:range];//从第3个字符开始到第16个字符
        NSLog(@"%@", string7);
        NSLog(@"%@", string8);
        NSLog(@"%@", string9);
//        检测前缀/后缀
//        - (BOOL)hasPrefix:(NSString *)aString;//是否以某个字符为前缀
        BOOL isPrefix = [string1 hasPrefix:@"http://www.baidu.com"];//检测某个字符串是否以百度链接开头
//        - (BOOL)hasSuffix:(NSString *)aString;//是否以某个字符为后缀
        BOOL isSuffix = [string1 hasSuffix:@".jpg"];//检测某个字符串是否以此后缀结尾,还可以检测域名是否以.cn/.com结尾,实行过滤
        NSLog(@"%d", isPrefix);
        NSLog(@"%d", isSuffix);
//        查找字符
//        - (NSUInteger)length;
//        - (unichar)characterAtIndex:(NSUInteger)index;
        char c = [string1 characterAtIndex:3];
        NSLog(@"%c", c);
//        查找字符串
//        - (NSRange)rangeOfString:(NSString *)aString;
        range = [string1 rangeOfString:@"aa"];//如何解决未找到的判断,tips:NSNotFound
//        判断是否找到
//        BOOL isFound = range.location != NSNotFound;//不等于NSNotFound就找到了
        NSLog(@"%lu %lu", range.location, range.length);//输出字符串的位置和长度
        NSLog(@"%@", NSStringFromRange(range));//调用字符串的类方法输出字符串的位置
//        iOS8查找:是否包含
//        BOOL isContain = [string1 containsString:@"is"];//iOS8才能用的方法
        
//        替换//如何做到不全部替换???
        NSString *string10 = [string1 stringByReplacingOccurrencesOfString:@"is" withString:@"are"];
        NSLog(@"%@", string10);
        
//        获得字符串长度
        NSInteger length = [string1 length];//getter方法
        NSInteger length1 = string1.length;//还可以用点语法来调用
        NSLog(@"%ld %ld", length, length1);
        
//        字符串比较
        BOOL isEqual = [string isEqualToString:@"123"];//判断值是否相等
        BOOL b = string == string3;//这种方法比较的是地址是否相等
        NSLog(@"%d %d", isEqual, b);
        
        NSComparisonResult result = [string compare:string3];//NSComparisonResult是枚举
        NSLog(@"%ld", result);//这种比较对大小写敏感
        switch (result) {
            case NSOrderedAscending:
                NSLog(@"升序");
                break;
            case NSOrderedSame:
                NSLog(@"相同");
                break;
            case NSOrderedDescending:
                NSLog(@"降序");
                break;
            default:
                break;
        }
        
        NSComparisonResult result1 = [string caseInsensitiveCompare:@"abc"];//对大小写不敏感,不区分大小写
        NSLog(@"%ld", result1);
        
//        转换大小写
        NSString *string11 = [string uppercaseString];//转换为大写
        NSString *string12 = [string lowercaseString];//小写
        NSLog(@"%@ %@", string11, string12);
//        转换类型
        int intValue = [@"123" intValue];//string转换成int
        float floatValue = [@"123.1" floatValue];
        double doubleValue = [@"1243sa53.34523asf" doubleValue];//遇到不合法字符就停下转换
        bool boolValue = [@"t" boolValue];//转换以后非零即为1//t(true)和y(yes)是1,其他字母为0
        NSLog(@"%d", intValue);
        NSLog(@"%f", floatValue);
        NSLog(@"%lf", doubleValue);
        NSLog(@"bool = %d", boolValue);
        
//        分割字符串
        NSArray *array = [string componentsSeparatedByString:@"-"];//将字符串按“-”分隔符来分隔成数组
        NSString *string13 = [array componentsJoinedByString:@"."];//将数组按照分隔符组成字符串
        NSLog(@"%@", string13);

#pragma mark - NSMutableString:NSString
//        可变字符串
//        NSString不可变,只能用一个新的字符串来接收操作结果
//        NSMutableString *mutableString = [NSString stringWithFormat:@"%@", string];//便利初始化
//        [mutableString appendFormat:@"123"];//这个方法属于NSMutableString的,但是却用NSString初始化的,NSString是不能变的,会崩溃
        
        NSMutableString *mutableString = [NSMutableString stringWithFormat:@"%@", string];
//        追加
        [mutableString appendFormat:@"123"];
//        根据初始化长度进行初始化
        NSMutableString *mutableString1 = [NSMutableString stringWithCapacity:10];
        NSMutableString *mutableString2 = [NSMutableString string];//用得最多的初始化方法
//        插入
        [mutableString1 insertString:@"abc" atIndex:0];//注意索引不要越界,下标从0开始
        NSLog(@"%@", mutableString1);
//        删除
        [mutableString deleteCharactersInRange:NSMakeRange(0, 3)];
//        修改
        [mutableString replaceCharactersInRange:NSMakeRange(0, 3) withString:@"123456235"];//后面的长度不用考虑是否越界
        NSLog(@"%@", mutableString);
//        - (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange;
    }
    return 0;
}

猜你喜欢

转载自yuandaimeng.iteye.com/blog/2200410