iOS基础一 NSString

本文探讨了Objective-C与Swift两种编程语言在iOS开发领域的应用与区别,包括其优势、使用场景及迁移策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NSString *string = @"Progamming is funny";
        NSLog(@"%@",string);
        
        NSString *str1 = @"this is string A";
        NSString *str2 = @"this is string B";
        NSString *res;
        NSComparisonResult comparionResult;
        
        //count the number of characters
        NSLog(@"Length of str1: %lu", [str1 length]);
        
        //copy one string to another
        res = [NSString stringWithString:str1];
        NSLog(@"copy: %@",res);
        
        //copy one string to the end of another
        str2 = [str1 stringByAppendingString:str2];
        NSLog(@"Conactenation: %@",str2);
        
        //Test if 2 strings are equal
        if([str1 isEqualToString:str2] == YES)
            NSLog(@"str1 == str2");
        else
            NSLog(@"str1 != str2");
        
        //Test if one string is <, ==, or > than another
        comparionResult = [str1 compare: str2];
        if(comparionResult == NSOrderedAscending)
            NSLog(@"str1 < str2");
        else if(comparionResult == NSOrderedSame)
            NSLog(@"str1 == str2");
        else
            NSLog(@"str1 > str2");
        
        //conver a string to upperCase
        res = [str1 uppercaseString];
        NSLog(@"%@",res);
        
        //conver a string to lowerCase
        res = [str1 lowercaseString];
        NSLog(@"%@", res);
        
        NSRange subRange;
        
        //Extact first 3 chars from string
        res = [str1 substringToIndex:3];
        NSLog(@"First 3 chars of str1: %@", res);
        
        //Extact chars to end of string starting at index 5
        res = [str1 substringFromIndex:5];
        NSLog(@"chars from index 5 of str1: %@",res);
        
        //Extact char from index 8 through 13 (6 chars)
        res = [[str1 substringFromIndex:8] substringToIndex:6];
        NSLog(@"chars from index 8 through 13: %@",res);
        
        //An easier way to do the same thing
        res = [str1 substringWithRange:NSMakeRange(8, 6)];
        NSLog(@"chars from index 8 through 13: %@",res);
        
        //locate one string inside another
        subRange = [str1 rangeOfString:@"string A"];
        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");
        else
            NSLog(@"string is at index %lu, length is %lu",subRange.location,subRange.length);
        
        ///mutable
        NSString *search,*replace;
        NSMutableString *mstr;
        NSRange subString;
        
        //create mutable string from nonmutable
        mstr = [NSMutableString stringWithString:str1];
        NSLog(@"%@",mstr);
        
        //Insert characters
        [mstr insertString:@" mutable" atIndex:7];
        NSLog(@"%@",mstr);
        
        //Effective contacentation if insert at end
        [mstr insertString:@" and string B" atIndex:[mstr length]];
        NSLog(@"%@",mstr);
        
        //or can user appendString directly
        [mstr appendString:@" and string C"];
        NSLog(@"%@",mstr);
        
        //delete substring based on range
        [mstr deleteCharactersInRange:NSMakeRange(16, 13)];
        NSLog(@"%@",mstr);
        
        //Find range first and the use it for delection
        subString = [mstr rangeOfString:@" string B and"];
        if(subString.location != NSNotFound){
            [mstr deleteCharactersInRange:subString];
            NSLog(@"%@",mstr);
        }
        
        //set the mutable string
        [mstr setString:@"This is string A"];
        NSLog(@"%@",mstr);
        
        //replace a range of chars with another
        [mstr replaceCharactersInRange:NSMakeRange(8, 8) withString:@"a mutable string"];
        NSLog(@"%@",mstr);
        
        //search and replace
        search = @"This is ";
        replace = @"An example of ";
        subString = [mstr rangeOfString:search];
        if(subString.location != NSNotFound){
            [mstr replaceCharactersInRange:subString withString:replace];
            NSLog(@"%@",mstr);
        }
        
        //search and replace all occurrences
        search = @"a";
        replace = @"X";
        subString = [mstr rangeOfString:search];
        while (subString.location != NSNotFound) {
            [mstr replaceCharactersInRange:subString withString:replace];
            subString = [mstr rangeOfString:search];
        }
        NSLog(@"%@",mstr);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值