user741076
user741076

Reputation: 63

How to remove special characters from NSString

I am doing push notification application and i am getting device token <8c09362c 82d6b735 c82fb2d9 8070db6f f73419b3 9da15e34 72aba570 6fbf5a45>, I got that device token from NSData, have succesfully converted into NSString, however i only need to remove first and last special character < > from NSString

Upvotes: 0

Views: 12839

Answers (3)

Kapil Choubisa
Kapil Choubisa

Reputation: 5232

There are lots of way to achieve this:

One you can use the way bigkm shows. Second Empty Stack had suggest a better way too.

Here is one other way:

NSString *dataToken = @"<8c09362c 82d6b735 c82fb2d9 8070db6f f73419b3 9da15e34 72aba570 6fbf5a45>";
NSString *str = [dataToken stringByReplacingOccurrencesOfString:@"<" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@">" withString:@""];

Upvotes: 3

EmptyStack
EmptyStack

Reputation: 51374

If you just want to trim certain characters from a string, you can use NSCharacterSet and stringByTrimmingCharactersInSet: method of NSString.

NSCharacterSet *chs = [NSCharacterSet characterSetWithCharactersInString:@"<>"];
string = [string stringByTrimmingCharactersInSet:chs];

Upvotes: 14

bigkm
bigkm

Reputation: 2277

simple

NSString *dataToken = @"<8c09362c 82d6b735 c82fb2d9 8070db6f f73419b3 9da15e34 72aba570 6fbf5a45>";
NSString *token = [dataToken substringWithRange:NSMakeRange(1, [dataToken length]-2)]; 

Upvotes: 1

Related Questions