Foundation框架支持一个名为NSString的类,用于处理字符串对象。
注意 : 要使用OC语言创建一个字符串对象,需要在字符串开头放置一个@字符:

@"Programming is fun"

一 : NSLog
%@用来显示NSString。

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str = @"Programming is fun";
        NSLog(@"%@",str);
    }
    return 0;
}

结果:
Objective-C之字符串对象

当然值得注意的是 , %@可以显示其他的对象:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str = @"Programming is fun";
        NSLog(@"%@",str);

        NSNumber *initNum = [NSNumber numberWithInteger:100];
        NSLog(@"%@",initNum);
    }
    return 0;
}

结果:
Objective-C之字符串对象

二 : 基本字符串操作①

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str1 = @"This is string A --";
        NSString *str2 = @"This is string B";

        NSString *res;
        NSComparisonResult compareResult;

        //计算字符串中的字符
        NSLog(@"Length of str1 : %lu" , [str1 length]);

        //将一个字符串复制到另一个字符串(全部覆盖(擦掉覆盖))
        res = [NSString stringWithString:str1];
        NSLog(@"copy : %@", res);
        res = [NSString stringWithString:str2];
        NSLog(@"copy : %@", res);

        //将一个字符串复制到另一个字符串的末尾(在str1后面加str2)
        str2 = [str1 stringByAppendingString:str2];
        NSLog(@"Concatentation : %@" , str2);

        //验证2个字符串是否相等
        if( [str1 isEqualToString: res] == YES){
            NSLog(@" str1 == res ");
        }else{
            res = [NSString stringWithString:str1];
            if([str1 isEqualToString:res] == YES){
                NSLog(@"str1 == res 2");
            }
        }

        //验证一个字符串死否小于,等于或大于另一个字符串

        compareResult = [str1 compare:str2];
        if( compareResult == NSOrderedAscending ){
            NSLog(@"str1 < str2");
        }else if( compareResult == NSOrderedSame ){
            NSLog(@"str1 == str2");
        }else{
            //NSOrderedDescending
            NSLog(@"str1 > str2");
        }

        //将字符串转为大写(注意 : str1的大小写不变)
        res = [str1 uppercaseString];
        NSLog(@"str1 : %@ , res : %@" , str1 , res);

        //将字符串转换成小写
        res = [str1 lowercaseString];
        NSLog(@"str1 : %@ , res : %@" , str1 , res);
    }
    return 0;
}

结果:

Objective-C之字符串对象

二 : 基本字符串操作②

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str1 = @"This is string A --";
        NSString *str2 = @"This is string B";

        NSString *res;
        NSRange subRange;

        //从字符串中提取前3个字符
        res = [str1 substringToIndex:3];
        NSLog(@"First 3 chars of str1 : %@" , res);

        //提取从索引5开始直到结尾的子字符串
        res = [str1 substringFromIndex:5];
        NSLog(@"Chars from index 5 of str1 : %@", res);

        //提取从索引5开始到索引13的子字符串(6个字符)
        res = [[str1 substringFromIndex:8] substringToIndex:6];
        NSLog(@"Chars from index 8 through 13 : %@" , res);

        //更简单的方法  提取从索引5开始到索引13的子字符串(6个字符)
        res = [str1 substringWithRange:NSMakeRange(8,6)];
        NSLog(@"!! Chars from index 8 through 13 : %@" , res);

        //查找字符串
        subRange = [str1 rangeOfString:@"string"];
        NSLog(@"String is at index %lu ,length is %lu" , subRange.location , subRange.length);

        subRange = [str1 rangeOfString:@"string B"];
        //没有找到
        if( subRange.location == NSNotFound ){
            NSLog(@"String not found");
        }
    }
    return 0;
}

结果:
Objective-C之字符串对象

三 :可变字符串
NSMutableString 类可以用来创建可以更改字符的字符串对象,它继承自NSString

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str1 = @"This is string A";
        NSString *search , *replace;
        NSMutableString *mstr;
        NSRange substr;

        //从不可变字符串创建可变字符串
        mstr = [NSMutableString stringWithString: str1];
        NSLog(@"%@",mstr);

        //插入字符
        [mstr insertString:@" Aonaufly" atIndex:7];
        NSLog(@"%@",mstr);

        //插入末尾进行有效拼接
        [mstr insertString:@" and string B" atIndex:[mstr length]];
        NSLog(@"%@",mstr);
        //same
        [mstr appendString:@" and string C"];
        NSLog(@"%@",mstr);

        //根据范围删除子字符串
        [mstr deleteCharactersInRange: NSMakeRange(16,13)];
        NSLog(@"%@",mstr);

        //查找然后将其删除
        substr = [mstr rangeOfString:@"string B and "];
        if( substr.location != NSNotFound ){
            [mstr deleteCharactersInRange:substr];
            NSLog(@"%@",mstr);
        }

        //直接设置为可变的字符串
        [mstr setString:@"This is string A"];
        NSLog(@"%@",mstr);

        //替换一些字符串
        [mstr replaceCharactersInRange:NSMakeRange(8,8) withString:@"a mutable string"];
        NSLog(@"%@",mstr);

        //查找和替换
        search = @"This is";
        replace = @"An example of";
        substr = [mstr rangeOfString:search];
        if(substr.location != NSNotFound){
            [mstr replaceCharactersInRange: substr withString:replace];
            NSLog(@"%@",mstr);
        }

        //查找和替换所有的匹配项
        search = @"a";
        replace = @"X";
        substr = [mstr rangeOfString:search];
        while (substr.location != NSNotFound) {
            [mstr replaceCharactersInRange:substr withString:replace];
            substr = [mstr rangeOfString:search];
        }
        NSLog(@"%@",mstr);
    }
    return 0;
}

结果:
Objective-C之字符串对象