开发者

Is there any possibility to use more than one NSCharacterSet Object for the same NSString?

Consider this code:

NSString *aString = @"\tThis is a sample string";
NSString *trimmed开发者_开发知识库String = [aString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"The trimmed string: %@",trimmedString);
trimmedString = [aString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"string"]];
NSLog(@"The trimmed string: %@",trimmedString);

Here if I use characterSetWithCharactersInString: on the same NSString object trimmedString, my previous whitespace trimming effect gets removed..

My question is,

Is there any possibility to use more than one NSCharacterSet object to the same NSString??? or Suggest me some other way to do this please, but the NSString object should be one and the same..


The problem is not because of character sets. Its because you are using aString while trimming the string second time. You should use trimmedString instead. Your code should look like,

trimmedString = [trimmedString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"string"]];


What about this:

NSString *aString = @"\tThis is a sample string";
NSMutableCharacterSet *customSet = [[NSMutableCharacterSet alloc] init];
[customSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
[customSet addCharactersInString:@"string"];
NSString *trimmedString = [aString stringByTrimmingCharactersInSet:customSet];
[customSet release];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜