替换特殊字符的问题 [英] Issue with replacing special characters

查看:121
本文介绍了替换特殊字符的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了从字符串中删除特殊字符的问题。我使用了以下代码。但是dint work.Please建议我更好的逻辑

Im having a issue with removing special characters from the string .I used the following code.But dint work.Please suggest me better logic

- (NSString *)trimmedReciString:(NSString *)stringName 
{
    NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"-/:;()$&@\".,?!\'[]{}#%^*+=_|~<>€£¥•."];
    for (int i = 0; i < [stringName length]; i++) {
        unichar c = [stringName characterAtIndex:i];
        if ([myCharSet characterIsMember:c]) {
            NSLog(@"%@",[NSString stringWithFormat:@"%c",[stringName characterAtIndex:i]]);
            stringName =  [stringName stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%c",[stringName characterAtIndex:i]] withString:@""];  

        }
    }
    return stringName;
}


推荐答案

NSString *s = @"$$$hgh$g%k&fg$$tw/-tg";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"-/:;()$&@\".,?!\'[]{}#%^*+=_|~<>€£¥•."];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
NSLog(@"String is: %@", s);

这篇关于替换特殊字符的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆