oc学习笔记4—— 字符串的常用方法

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        //创建字符串的几种方式
        //直接用NSString创建
        NSString *s=@"hello";
        NSString *ts1=[[NSString alloc ] initWithString:s];
        NSLog(@"%@",ts1);
        //通过c的字符串创建
        NSString *ts2=[[NSString alloc]initWithUTF8String:"用c的字符串,创建字符串"];
        NSLog(@"%@",ts2);
        //使用格式符创建
        NSString *ts3=[[NSString alloc]initWithFormat:@"格式符,拼接字符串%@",s];
        NSLog(@"%@",ts3);
        
        //临时字符串
        NSString *ts4=[NSString stringWithFormat:@"%@%d%c",@"hello ",5,'A'];
        NSLog(@"%@",ts4);
        NSString *ts5=[NSString stringWithString:s];
        NSLog(@"%@",ts5);
        NSString *ts6=[NSString stringWithUTF8String:"c自负串"];
        NSLog(@"%@\n\n\n",ts6);
        
        //字符串的比较
        NSString *ns1=@"123";
        NSString *ns2=@"456 ";
        //判断两个字符串是不是相等
        NSLog(@"%d",[ns1 isEqualToString:ns2]);
        
        //compare 方法可以返回NSComparisonResult  进行判断字符串的大小
        NSComparisonResult ret=[ns1 compare:ns2];
        if (ret==NSOrderedAscending) {
            NSLog(@"ns1<ns2");
        }else if(ret==NSOrderedSame)
        {
            NSLog(@"ns1==ns2");
        }else if(ret==NSOrderedDescending)
        {
            NSLog(@"ns1>ns2");
        }
        
         //查找子字符串
         NSLog(@"n\n\n 查找子字符串");
        NSString *str=@"i am a so bad man";
        NSString *str2=@"so";
        //NSRange 是个结构体  又两个属性 length 表示长度 location表示位置
        //查找结果是NSRange
        NSRange range=[str rangeOfString:str2];
        NSLog(@"%lu   %lu",range.length,range.location);
        
        //截取子字符串
        NSLog(@"n\n\n 截取子字符串");
        NSString *st=@"1234567890";
        NSString *subSt=[st substringWithRange:range];
        NSLog(@"%@",subSt);
        NSString *subst2=[st substringFromIndex:5];
        NSLog(@"%@",subst2);
        NSString *substr3=[st substringToIndex:5];
        NSLog(@"%@\n\n",substr3);
        
        
        
        
        NSLog(@"%@",str);
        //单词首字母大写
        NSLog(@"%@",[str capitalizedString]);
        //单词全部大写
        NSLog(@"%@",[str uppercaseString]);
        //单词首字母小写
        NSLog(@"%@",[str lowercaseString]);
        
        //文件的拓展名
        NSString *Path = @"~/NSData.txt";
        NSLog(@"Extension:%@",[Path pathExtension]);
        
    }
    return 0;
}
</pre><pre name="code" class="objc">
<pre name="code" class="objc">#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        NSMutableString *mustr=[[NSMutableString alloc] initWithString:@"This is a "];
        NSLog(@"%@",mustr);
        //在字符串的末尾添加字符串
        [mustr appendString:@"NSMutableString"];
        NSLog(@"%@",mustr);
        //在字符串的末尾添加格式化字符串
        [mustr appendFormat:@" %d",100];
        NSLog(@"%@",mustr);
        
        //在index添加字符串
        [mustr insertString:@"Hello! " atIndex:0];
        NSLog(@"%@",mustr);
        
        //检查字符串是不是 以某个字符串开头
        NSLog(@"%d",[mustr hasPrefix:@"Hello"]);
        
        //检查字符串是不是 以某个字符串结尾
        NSLog(@"%d",[mustr hasSuffix:@"100"]);
        
        // 替换单词
        [mustr replaceCharactersInRange:NSMakeRange(2, 4) withString:@"XXX"];
        NSLog(@"%@",mustr);
        
        //重置添加新的字符串
        [mustr setString:@"Tom"];
        NSLog(@"%@",mustr);
    }
    return 0;
}


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值