IOS rookie beginner CHAPTER 12: ios url transcoding appeared unable to remove spaces in the string (for some spaces can not be selected)

As shown in, string manipulation and I transcoding url link as FIG gray blocks, I can not in any case the result that a gray removed

Later, after the attempt, not a full-size space, and when the removal of a tab -> "\ t" the successful elimination of these a blank, as

So, ios right urlencode as follows:

/**
 url转码字符串
 
 @param str 需要转码的字符串
 @return 已完成转码的字符串
 */
-(NSString *) encodingStringOfUrl:(NSString *) str{
    // 去除特殊字符
    NSString * charaters = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\| ";
    NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:charaters] invertedSet];
    // 去除空格
    NSString *temp = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    // 去除回车
    temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    // 去除换行
    temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    // 去除制表符
    temp = [temp stringByReplacingOccurrencesOfString:@"\t" withString:@""];
    NSString * encodeStr = [temp stringByAddingPercentEncodingWithAllowedCharacters:set];
    return encodeStr;
}

 

Published 65 original articles · won praise 31 · views 50000 +

Guess you like

Origin blog.csdn.net/zhangtao0417/article/details/92793025