I want to check if a particular string is just made up of spaces. It could be any number of spaces, including zero. What is the best way to determine that?
You can check if [string length] == 0 . This will check if it's a valid but empty string (@"") as well as if it's nil, since calling length on nil will also return 0. There are some very rare NSStrings where this will result in a false negative (saying the string isn't empty, when, for practical purposes, it is).
NSString is a classSwift is interoperatable with Objective-C and converts some Objective-C types to Swift types. Types that can be converted between Obj-C and Swift are known as bridged types. String and NSString are example of such bridged types and hence you can assign NSString to a String variable.
Objective-C is the primary programming language you use when writing software for OS X and iOS. It's a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime.
A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.
NSString *str = @" "; NSCharacterSet *set = [NSCharacterSet whitespaceCharacterSet]; if ([[str stringByTrimmingCharactersInSet: set] length] == 0) { // String contains only whitespace. }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With