如何删除前导和后导NSArray 中 NSString 的尾随空格?
我有一个 NSArray 声明如下:
@property (nonatomic, strong) NSArray *arrayRefineSubjectCode;
我手动填写了数组元素,如下所示:
arrayRefineSubjectCode = [NSArray arrayWithObjects:
@" BKKC 2061",
@" BKKS 2631 ",
@"BKKS 2381 ",
nil];
那么如何删除开始和结束空格并使每个数组元素变为如下所示:
arrayRefineSubjectCode = [NSArray arrayWithObjects:
@"BKKC 2061",
@"BKKS 2631",
@"BKKS 2381",
nil];
我尝试过使用“stringByTrimmingCharactersInSet:”,但它只能工作对于 NSString。这里有点困惑。请帮忙...
I have an NSArray declared as such:
@property (nonatomic, strong) NSArray *arrayRefineSubjectCode;
I have the array elements manually filled out as below:
arrayRefineSubjectCode = [NSArray arrayWithObjects:
@" BKKC 2061",
@" BKKS 2631 ",
@"BKKS 2381 ",
nil];
So how do I remove starting and ending whitespace and make each array elements to become as these:
arrayRefineSubjectCode = [NSArray arrayWithObjects:
@"BKKC 2061",
@"BKKS 2631",
@"BKKS 2381",
nil];
I have tried using "stringByTrimmingCharactersInSet:" but it only works for NSString. Kinda confused here. Please help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
NSArray
和包含的NSString
对象都是不可变的。没有办法改变你拥有的对象。相反,您必须创建新字符串并将它们放入新数组中:
The
NSArray
and the containedNSString
objects are all immutable. There's no way to change the objects you have.Instead you have to create new strings and put them in a new array:
阅读http://nshipster.com/nscharacterset/
所以,在 Swift 3 中
Read http://nshipster.com/nscharacterset/
So, in Swift 3
SSToolkit 有几个不错的类别为此:编辑:链接已损坏,并且似乎不是不再在 SSToolkit 中。这是旧代码:
但是,最好创建要删除的所有字符集的并集,而不是使用不同的字符集进行多次剥离。
NSMutableCharacterSet
是你的朋友。SSToolkit has a couple of nice categories for this:edit: link is broken and it doesn't seem to be in SSToolkit anymore.This is the old code:
However instead of stripping several times with different character sets, it might be better to create a union of all character sets you want removed.
NSMutableCharacterSet
is your friend there.尼古拉关于可变性的看法是正确的。因此,解决问题的最简单方法可能是定义
然后一一插入字符串,例如
......获得三个带有“blah”的元素。请注意,您不能将对象添加到不可变的 NSArray,只能添加到可变的 NSMutableArray。
当然,您可能已经在某处放置了带有多余空格的字符串。您必须创建这些字符串的可变 (!) 副本,并使用 addObject 将它们添加到 arrayRefineSubjectCode 数组中。您可以在将它们添加到数组之前或之后删除空格。
希望能有所帮助。
只是想添加最后一句话。您可能想知道为什么要使用不可变对象。有几个原因,但如果你可以摆脱不可变,它们会导致更快的代码,复制很容易(只需将指针复制到保存数据的地址,因为该数据无论如何都不会改变),并且它是更有可能是线程安全的。当然,要小心 NSArray 指向像 NSMutableString 这样的可变对象!
Nikolai is right about the mutability. So the probably easiest way to solve things is to define
and then insert strings one by one, e.g.
... to get three elements with " blah " in it. Note that you cannot addObject to an immutable NSArray, only to a mutable NSMutableArray.
Of course, you may have the strings with superfluous spaces sitting around somewhere already. You'll have to make a mutable (!) copy of those strings, and add those to the arrayRefineSubjectCode array with addObject. You could remove the spaces before or after adding them to the array.
Hope that helps a bit.
Just thought to add a last remark. You might wonder why you'd use immutable objects anyway. There are a few reasons, but if you can get away with immutable, they result in faster code, copying is easy (just copy the pointer to the address that holds the data, because that data won't change anyway), and it is more likely to be thread-safe. Of course, be careful then with an NSArray that points at mutable objects like NSMutableString!
从“string”中删除前导空格和尾随空格
Removes leading white spaces and trailing white spaces from "string"
改变 no,复制并替换 yes:
Mutate no, copy and replace yes: