开发者

How to remove special characters from NSString

开发者 https://www.devze.com 2023-04-08 06:55 出处:网络
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 succesful

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 convert开发者_运维问答ed into NSString, however i only need to remove first and last special character < > from NSString


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];


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:@""];


simple

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

精彩评论

暂无评论...
验证码 换一张
取 消