是否有可能为同一个 NSString 使用多个 NSCharacterSet 对象?

发布于 2024-12-04 16:14:48 字数 728 浏览 1 评论 0原文

考虑这段代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落□心尘 2024-12-11 16:14:49

问题不是因为字符集。这是因为您在第二次修剪字符串时使用了 aString 。您应该使用 trimmedString 来代替。你的代码应该看起来像,

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

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"]];
溺渁∝ 2024-12-11 16:14:49

这个怎么样:

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文