温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Objective-C之字符串对象

发布时间:2020-07-02 13:01:47 来源:网络 阅读:776 作者:Aonaufly 栏目:开发技术

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之字符串对象

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI