是否有可能为同一个 NSString 使用多个 NSCharacterSet 对象?
考虑这段代码:
NSString *aString = @"\tThis is a sample string";
NSString *trimmedString = [aString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"The trimmed string: %@",trimmedString);
trimmedString = [aString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"string"]];
NSLog(@"The trimmed string: %@",trimmedString);
这里,如果我在同一个 NSString
对象 trimmedString
上使用 characterSetWithCharactersInString:
,我之前的 whitespace
修剪效果被删除了..
我的问题是,
是否有可能对同一个 NSString
使用多个 NSCharacterSet
对象??? 或者请建议我使用其他方法来执行此操作,但是 NSString
对象应该是同一个。
Consider this code:
NSString *aString = @"\tThis is a sample string";
NSString *trimmedString = [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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题不是因为字符集。这是因为您在第二次修剪字符串时使用了
aString
。您应该使用trimmedString
来代替。你的代码应该看起来像,The problem is not because of character sets. Its because you are using
aString
while trimming the string second time. You should usetrimmedString
instead. Your code should look like,这个怎么样:
What about this: