To trim a string and remove whitespace and newlines:
The String trim in Objective-C/Cocoa article by Aral Balkan, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial 2.0 UK: England License.
NSString *trimmedString = [dirtyString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
For other character sets, see this reference page.
The String trim in Objective-C/Cocoa article by Aral Balkan, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial 2.0 UK: England License.
I still don’t understand why that language is so complex…
It’s an interesting one, jankees. It’s basically C with an Smalltalk-esque messaging/OO layer. The language itself is rather easy; I believe you’re balking at the conventions of the Cocoa framework (which, of course, are somewhat born out of the messaging syntax in Obj-C). Thing is, Cocoa is a very good example of literate programming: method signatures may be ridiculously long but you’re not supposed to type them in yourself (XCode either auto-completes for you, or, when implementing delegate/datasource methods, you copy the method signatures out of the documentation). The documentation is an *essential* part of the development process (compare to Python, for example, where you don’t need documentation, especially if you have the iPython shell, or to AS3 where you *do* need documentation and don’t have it!) :)
So, yes, it is a framework with verbose conventions but the whole system (IDE, documentation, etc. works really well together). I do enjoy developing with.
Hi Aral,
Thanks for the reply, I’m still more into AS3 at the moment, but maybe I should spend some more time on Cocoa. Do you have any good starting points? Where did you start?
Thanks,
Jankees
Is this trimmed string autoreleased? Or should I release it after using it?
[...] 截去字符串的前后空格符。参考。 [...]
Awesome. Thanks for publishing this, just what I needed.
thanks for this simple line… =)
Thanks, this helped me. But: What I think the commenter means by “complex” is:
Trim a string in Java:
ss = ss.trim();
Trim a string in Apple:
ss = [ss stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Just venting. I’m from the Java world and can’t wait to go back. :)
It’s true that ss.trim() looks easier, but tell me, what, exactly, are you trimming? Is it just white space? I’m sure there are defaults for when nothing is specified, but at least in Obj-C/Cocoa, you know EXACTLY what’s going to happen.
I’m from the .NET world, and string trimming is an example of frustration for me, because it’s hard to figure out exactly what happens in each case. Not with Cocoa.
Mike – Your response is reasonable. But I’d argue that 98% of the time, yes, you really just want to trim whitespace. What’s more elegant and easier to understand and maintain:
car.startEngine();
or
[car startInternalCombustionGasolinePoweredEngineWithReason:[NSReasonForStarting toOperateOnPublicRoadways]];