user1705351
user1705351

Reputation:

Remove Special Characters in NSString

i am convert paragraph into words it contains many special characters like

"  , "  . `

how to remove this characters in nsstring and get only alphabets in nsstring

ex "new" to new //the special characters are change

Upvotes: 17

Views: 34685

Answers (5)

green0range
green0range

Reputation: 607

Per comment of @Rostyslav Druzhchenko from on the selected answer of @MadhuP:

NSString *unfilteredString = @"!@#$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
NSCharacterSet *notAllowedChars = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
NSString *escapedString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
NSLog (@"Result: %@", escapedString);

This is the answer that will use alphanumericCharacterSet to handle multiple countries character set.

Upvotes: 4

Andrea
Andrea

Reputation: 26385

Accepted answer in SWIFT (but not SWIFTY way):

 let notAllowedCharactersSet = NSCharacterSet(charactersInString: "ABCDEFGHIJKLMNOPQRSTUVWXYZ").invertedSet
 let filtered = (stringToFilter.componentsSeparatedByCharactersInSet(notAllowedCharactersSet) as NSArray).componentsJoinedByString("")

Upvotes: 0

dstefanis
dstefanis

Reputation: 247

I'm sure there's a more elegant solution, but for anyone trying to do this in Swift, here's what I did to make sure there were no special characters in my users' phone numbers.

var phone = "+1 (555) 555 - 5555"
var removeChars: NSCharacterSet = NSCharacterSet(charactersInString: "1234567890").invertedSet
var charArray = phone.componentsSeparatedByCharactersInSet(removeChars)
var placeholderString = ""
var formattedPhoneNumber: String = placeholderString.join(charArray).stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())

The stringByTrimmingCharactersInSet might not be necessary.

Upvotes: -1

MadhuP
MadhuP

Reputation: 2019

NSString *unfilteredString = @"!@#$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"] invertedSet];
NSString *resultString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
NSLog (@"Result: %@", resultString);

TRY THIS IT MAY HELPS YOU

Upvotes: 82

FluffulousChimp
FluffulousChimp

Reputation: 9185

There are numerous ways of dealing with this. As an example, here's a solution using regular expressions. This is just an example. We don't know the entire range of special characters that you want to remove.

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"[,\\.`\"]"
                                                                                    options:0
                                                                                      error:NULL];
        NSString *sampleString = @"The \"new\" quick brown fox, who jumped over the lazy dog.";
        NSString *cleanedString = [expression stringByReplacingMatchesInString:sampleString
                                                                       options:0
                                                                         range:NSMakeRange(0, sampleString.length)
                                                                  withTemplate:@""];
        printf("cleaned = %s",[cleanedString UTF8String] );

    }
    return 0;
}

Upvotes: 5

Related Questions