美文网首页
2023-07-21

2023-07-21

作者: Fenghao_耗子 | 来源:发表于2023-07-20 09:31 被阅读0次

    iOS NSString常用用法大全
    一、NSRange

    在对NSString介绍之前,我们先要了解一个结构体NSRange。

    typedef struct _NSRange
    {
    unsignedint location;

    unsignedint length;  
    

    }NSRange;

    NSRange表示相关事物的一个范围,常与字符串操作一起使用。

    Location时字段的起始位置,length为字段的长度。

    例如:

    NSRange range = [@"I am atany" rangeOfString:@"am"];//查找“am”字符串在“I am atany”中的位置。

    NSLog(@"%d,%d",range.location,range.length);//2,2

    声明一个NSRange有几种方法。

    第一种:

    NSRange range;

    range.location = 0;

    range.length = 1;
    第二种:

    NSRange range = {0,1};
    第三种:

    NSRange range = NSMakeRange(0, 1);

    二、NSString

    下面介绍一些NSString常用的方法。

    1、字符串的声明

    1)直接赋值常量

    NSString *string1 = @"hello";
    2)先分配内存初始化再赋值

    NSString *string2 = [[NSStringalloc]init];

    string2= @"hello";
    3)使用工厂方法制造对象

    NSString *string3 = [NSStringstringWithFormat:@"hello"];
    4)使用initWithString与initWithFormat方法初始化

    NSString *string4 = [[NSStringalloc]initWithString:@"hello"];

    NSString *string4 = [[NSStringalloc]initWithFormat:@"hello"];
    5)使用C字符串生成NSString

    NSString *string5 = [[NSStringalloc]initWithCString:"hello"encoding:NSUTF8StringEncoding];
    6)读取本地文件里面的字符串,hello.text为相对路径,存放在项目中

    NSString *path = [[NSBundlemainBundle] pathForResource:@"hello.txt"ofType:nil];

    NSString *string6 = [[NSStringalloc] initWithContentsOfFile:path encoding:NSUTF8StringEncodingerror:nil];
    7)使用已有的字符串生成

    NSString *string7 = [NSStringstringWithString:string6];

    2、获取字符串的长度

    NSString *str = [NSString stringWithFormat:@"first"];

    NSLog(@"str length is %d",str.length); //字符串长度

    3、转化字符串的大小写

    NSString *string = @"hEllO";

    NSLog(@"string is %@",[string uppercaseString]);//转化为大写

    NSLog(@"string is %@",[string lowercaseString]);//转化为小写

    NSLog(@"string is %@",[string capitalizedString]);//转化为首字母大写
    输出:

    string is HELLO

    string is hello

    stringis Hello

    4、判断字符串前缀与后缀

    NSString *hello = @"hello.png";//0代表匹配,1代表不匹配

    NSLog(@"hello has prefix %d",[hello hasPrefix:@"hello"]);//以hello开头

    NSLog(@"hello has suffix %d",[hello hasSuffix:@".png"]);//以.png结尾
    可以方便的实现判断文件类型等操作。

    5、判断字符串是否相等

    如果相等,会返回YES,否则NO
    

    NSString *string1 = @"hello";

    NSString *string2 = [NSString stringWithFormat:@"hello"];

    NSString *string3 = @"hi";

    if([string1 isEqualToString:string2]){
    NSLog(@"string1 is Same to string2");
    }
    else{
    NSLog(@"string1 is different to string2");
    }

    if([string1 isEqualToString:string3]){
    NSLog(@"string2 is Same to string3");
    }
    else{
    NSLog(@"string2 is different to string3");
    }
    输出结果:

    string1 is Same to string2

    string2 is different to string3

    6、包含其他字符串

    使用rangeOfString返回只一个NSRange参数,通过range的location与length可以方便的找到包含字符串
    

    NSString *string1 = @"hello";

    NSString *string2 = @"I am hello123";

    NSRange range = [string2 rangeOfString:string1];

    NSLog(@"range.location is %d,range.length is %d",range.location,range.length);
    输出:

    range.location is 5,range.length is 5

    如果不包含字符串,那么在range.location会等于NSNotFound

    7、字符串的比较

    字符串的比较使用 compare的方法

    返回值:NSComparisonResult

    ,[@"hello" compare:@"hello"]);

    NSLog(@"flag is %d",[@"hello" compare:@"aello"]);

    NSLog(@"flag is %d",[@"hello" compare:@"hello1"]);

    NSLog(@"flag is %d",[@"hello" compare:@"Hello"]);//h在H之后
    结果为:

    flag is 0

    flag is 1

    flag is -1

    flag is 1

    8、不区分大小写的比较

    使用compare:options,options参数是一个掩位码,可以用“|”符号使用多个。

    NSCaseInsensitiveSearch:不区分大小写

    NSLiteralSearch:区分大小写

    NSNumericSearch:比较字符串的个数,而不是子字符值

    NSLog(@"%d",[@"hello" compare:@"Hello" options:NSCaseInsensitiveSearch]);//返回值为0

    NSLog(@"%d",[@"hello" compare:@"Hello" options: NSLiteralSearch]);//返回值为1

    NSLog(@"%d",[@"Name7.txt" compare:@"Name25.txt" options:NSNumericSearch]); //返回值为-1

    NSLog(@"%d",[@"Name7.txt" compare:@"Name25.txt"]);//返回值为1

    9、截取字符串

    NSString *string = @"hello world";

    NSString *string2 = [string substringToIndex:2];//2为长度(从0开始截取)

    NSLog(@"string2 is %@",string2);

    NSString *string3 = [string substringFromIndex:2];//从2开始(截取到最后)

    NSLog(@"string3 is %@",string3);

    NSRange range = { 2,2 };//从2开始截取,截取2位

    NSString *string4 = [string substringWithRange:range];

    NSLog(@"string4 is %@",string4);
    输出:

    string2 is he

    string3 is llo world

    string4 is ll

    10、去除字符串首尾的空格

    NSString *text = [@" hello " stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    NSLog(text);//hello

    11、NSMutableString

    NSString创建之后不可变的,而NSMutableString为可变的,类似与java中的string与stringBuffer。

    NSMutableString为NSString子类,继承所有方法。

    1)创建字符串

    50这个值只是一个参考,不够的话系统会自动补充。

    NSMutableString *string = [NSMutableStringstringWithCapacity:50];
    2)在字符串尾添加新的字符串

    [string appendString:@"hi"];

    NSLog(@"string is %@",string);//string is hi
    3)使用Format形式添加新的字符串

    [string appendFormat:@" I am %@",@"atany"];

    NSLog(@"string is %@",string);//string is hi I am atany
    4)删除字符串

    NSRange range = [string rangeOfString:@"am"];//找回”am”字符串位置(5,2)

    [string deleteCharactersInRange:range];//先用rangeOfString来得到range

    NSLog(@"string is %@",string);// string is hi I atany
    5)在字符串中插入字符串

    [string insertString:@"York"atIndex:1];//插入到h之后

    NSLog(@"string is %@",string);// string is hYorki I atany

    [string setString:@"replace"];//改变字符串

    NSLog(@"string is %@",string);// string is replace

    [string replaceCharactersInRange:range withString:@"is"];

    NSLog(@"string is %@",string);// string is replais
    注:range只是记录的一个字符串位置,而不是字符串,此时range为上次rangeOfString:@"am"的位置(5,2)。


    atany原创,转载请注明博主与博文链接,

                                                                  [http://blog.csdn.net/yang8456211/article/details/11744505](http://blog.csdn.net/yang8456211/article/details/11744505) [](http://blog.csdn.net/yang8456211/article/details/11542455) 
    

    —— by atany

                                                                        **************************************************************
    

    相关文章

      网友评论

          本文标题:2023-07-21

          本文链接:https://www.haomeiwen.com/subject/alaaudtx.html