Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if an NSString is just made out of spaces

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?

like image 452
Suchi Avatar asked Sep 01 '11 19:09

Suchi


People also ask

How do I know if NSString is empty?

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).

What is the difference between NSString and string?

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.

What is OBJC?

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.

What does NSString mean?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.


1 Answers

NSString *str = @"         "; NSCharacterSet *set = [NSCharacterSet whitespaceCharacterSet]; if ([[str stringByTrimmingCharactersInSet: set] length] == 0) {     // String contains only whitespace. } 
like image 91
Alexsander Akers Avatar answered Sep 20 '22 22:09

Alexsander Akers