NSString几个函数

第一、判断是否包含前缀于后缀    

   NSString *str=@"hello andy yang";

        if([str hasPrefix:@"hello"])

        {

            NSLog(@"the prefix is hello");

        }

        if([str hasSuffix:@"yang"])

        {

            NSLog(@"the suffix is yang");

        }

第二、NSRange

 

typedefstruct _NSRange {

     NSUInteger location;  //开始位置

    NSUInteger length;  //长度

} NSRange;

NSRange的定义:

 

        NSRange range2;

        range2.location = 17;

        range2.length = 4;

        NSLog(@"%lu and  %lu",range2.location,range2.length);

        //NSMakeRange来初始化

        NSRange rang1=NSMakeRange(1223);

        NSLog(@" %lu and %lu",rang1.location,rang1.length);

第三、 rangeOfString函数的使用

       NSRange range;

      range=[str rangeOfString:@"andy"];

        NSLog(@"%lu and  %lu",range.location,range.length);


第四、compare实现函数的比较        

        NSString *str1=@"AA";

        NSString *str2=@"aBB";

        if ([str2 compare:str1])

        {

            NSLog(@"%@ is larger than %@",str1,str2);

        }else

        {

            NSLog(@"%@ is larger than %@",str2,str1);

        }

第五、NSMutabelString 继承自NSString

 - (void)appendString:(NSString *)aString;

 

- (void)appendFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);

sample code:

        NSMutableString *str=[ NSMutableString stringWithCapacity : 12 ];

 

        [str appendString:@"hello"];

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

        

        [str appendFormat:@" andy yang %d",11];

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

   

- (void)deleteCharactersInRange:(NSRange)range; 删除字符串

        NSMutableString *str1=[NSMutableString stringWithFormat:

        @"hello what is your name?"];

        NSRange range;

        range=[str1 rangeOfString:@"what"];

        NSLog(@"start is %lu,length is %lu",range.location,range.length);

        [str1 deleteCharactersInRange:range];

        NSLog(@"str is %@",str1);

 

posted on 2013-11-14 20:59  you Richer  阅读(276)  评论(0编辑  收藏  举报