字符串的截取

//判断字符串是否有此前缀

        NSString *str=@"IOS8.jpg";

        BOOL result=[str hasPrefix:@"IOS"];//hasPrefix 只能判断前缀,从第0个位置比较

        //判断字符串是否由此后缀

        BOOL resu=[str hasSuffix:@".jpg"];//hasSuffix 只能判断后缀,从最后一个位置比较

        NSString *str1=@"apple banana pear";

        NSArray *array=[str1 componentsSeparatedByString:@" "];

        NSLog(@"%@",array);

        NSString *str2=@"abcdefg";

        NSString *st=[str2 substringToIndex:4];//substringToIndex 从字符串开始位置,一直截取到你指定的位置,不包含你指定的位置

        NSLog(@"%@",st);

        NSString *st1=[str2 substringFromIndex:4];//substringFromIndex 从你指定的位置开始截取,一直截取到字符串最后一位,包含你指定的位置

        NSLog(@"%@",st1);

        //根据范围截取字符串

        NSRange rang;

        rang.location=2;//从你指定的位置开始

        rang.length=3;//截取的长度

        NSString *st2=[str2 substringWithRange:rang];

        NSLog(@"%@",st2);

        NSString *str3=@"http://localhost:8099/test/user=admin&pwd=123";

        NSRange ran=[str3 rangeOfString:@"pwd="];

        NSLog(@"location=%ld,length=%ld",ran.location,ran.length);

posted @ 2016-01-06 16:59  5个半柠檬C  阅读(247)  评论(0编辑  收藏  举报