如何去掉String的前后空格或某些字符

06-26 12:33:00   来源:   评论: 点击:

import <Foundation Foundation h>@interface NSString (TrimmingAdditions) ** * 去掉字符串左边的特
#import <Foundation/Foundation.h>@interface NSString (TrimmingAdditions)/** *  去掉字符串左边的特定字符 * *  @param characterSet 需要去除的特定字符集 * *  @return 去除后的字符串 */- (NSString *)stringByTrimmingLeftCharactersInSet:(NSCharacterSet *)characterSet;/** *  去掉字符串右边的特定字符 * *  @param characterSet 需要去除的特定字符集 * *  @return 去除后的字符串 */- (NSString *)stringByTrimmingRightCharactersInSet:(NSCharacterSet *)characterSet;@end


@implementation NSString (TrimmingAdditions)- (NSString *)stringByTrimmingLeftCharactersInSet:(NSCharacterSet *)characterSet {    NSUInteger location = 0;    NSUInteger length = [self length];    unichar charBuffer[length];    //This method is unsafe because it could potentially cause buffer overruns.    //[self getCharacters:charBuffer];    [self getCharacters:charBuffer range:NSMakeRange(location, length)];    for (location = 0; location < length; location++) {        // charBuffer[i] 是 字符对应的ASCII值        //DLog(@"charBuffer = %hu", charBuffer[location]);        if (![characterSet characterIsMember:charBuffer[location]]) {            break;        }    }    return [self substringWithRange:NSMakeRange(location, length - location)];}- (NSString *)stringByTrimmingRightCharactersInSet:(NSCharacterSet *)characterSet {    NSUInteger location = 0;    NSUInteger length = [self length];    unichar charBuffer[length];    //[self getCharacters:charBuffer];    [self getCharacters:charBuffer range:NSMakeRange(location, length)];    for (length = [self length]; length > 0; length--) {        if (![characterSet characterIsMember:charBuffer[length - 1]]) {            break ;        }    }    return [self substringWithRange:NSMakeRange(location, length - location)];}@end

相关热词搜索:

上一篇:iOS - 类似朋友圈的社交界面实现 下一篇:iOS 中的 GpuImage 及相关滤镜介绍
分享到: 收藏