views:

21

answers:

1

Hi,

It is exasperating but I can't get this code work, first I though it was a mutable/inmutable problem but it doesn't, I believe. What am I doing wrong? labeledPemString do has contents and the specified characterSet is printing 'ranges begin {0, 26}' . But unlabeledBeginPemString has the same as the original string.

//Get the .pem file contents
 NSString *path = [[NSBundle mainBundle] pathForResource:@"publickey" ofType:@"pem"];
 NSMutableString *labeledPemString = [[NSMutableString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
 NSLog(@"labeled  %@", labeledPemString);
 //Take off -----BEGIN PUBLIC KEY----- and -----END PUBLIC KEY----- headers
 //First get begin range
 NSRange beginHeaderRange = [labeledPemString rangeOfString:@"-----BEGIN PUBLIC KEY-----"];
 NSLog(@"ranges begin %@ ", NSStringFromRange(beginHeaderRange));

 //Create a characterset with begin range
 NSCharacterSet *beginHeaderChSet = [NSCharacterSet characterSetWithRange:beginHeaderRange];
 //Trim text
 NSMutableString *unlabeledBeginPemString = [[labeledPemString stringByTrimmingCharactersInSet:beginHeaderChSet] mutableCopy];
 NSLog(@"unlabeled 1 %@", unlabeledBeginPemString);

Thanks for your help.

A: 

stringByTrimmingCharactersInSet: removes the occurrences of characters in the set from the receiver. i.e. if the NSCharacterSet contains a and c, and if you do

[@"abc" stringByTrimmingCharactersInSet:characterSet];

you get @"b" and the range in characterSetWithRange: is the range of the unicode to be put in the set of characters.

I guess this is not what you want. If you just want to remove substrings from a given string, just use substringFromIndex:, substringToIndex and substringWithRange:.

Yuji
Thanks a lot Yuji !!! I completely forgot those methods ...
iPhoneDevProf