Believe in your own future, will thank yourself right now Sinner Yun

Sinner_Yun

字符串删减

 

 
*----------------在串中搜索子串----------------*/        

    NSString *string1 = @"This is a string";
    NSString *string2 = @"string";
    NSRange range = [string1 rangeOfString:string2];
    int location = range.location;
    int leight = range.length;
    NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]];
    NSLog(@"astring:%@",astring);
    [astring release];


    /*----------------抽取子串 ----------------*/        

    //-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
    NSString *string1 = @"This is a string";
    NSString *string2 = [string1 substringToIndex:3];
    NSLog(@"string2:%@",string2);




    //-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
    NSString *string1 = @"This is a string";
    NSString *string2 = [string1 substringFromIndex:3];
    NSLog(@"string2:%@",string2);
 
    //-substringWithRange: //分割字符串成数组
    NSString *string1 = @"This is a string";
    NSArray *string2 = [string1 componentsSeparatedByString:@"-"];

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


    //-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串
    NSString *string1 = @"This is a string";
    NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];
    NSLog(@"string2:%@",string2); 
 

posted on 2014-05-11 17:05  Sinner_Yun  阅读(375)  评论(0编辑  收藏  举报

导航