Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 382 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I test if a string is empty in Objective-C?

#11
So aside from the basic concept of checking for a string length less than 1, it is important to consider context deeply.
Languages human or computer or otherwise might have different definitions of empty strings and within those same languages, additional context may further change the meaning.

Let's say empty string means "a string which does not contain any characters significant in the current context".

This could mean visually, as in color and background color are same in an attributed string. Effectively empty.

This could mean empty of meaningful characters. All dots or all dashes or all underscores might be considered empty.
Further, empty of meaningful significant characters could mean a string that has no characters the reader understands.
They could be characters in a language or characterSet defined as meaningless to the reader. We could define it a little differently to say the string forms no known words in a given language.

We could say empty is a function of the percentage of negative space in the glyphs rendered.

Even a sequence of non printable characters with no general visual representation is not truly empty. Control characters come to mind. Especially the low ASCII range (I'm surprised nobody mentioned those as they hose lots of systems and are not whitespace as they normally have no glyphs and no visual metrics). Yet the string length is not zero.

Conclusion.
Length alone is not the only measure here.
Contextual set membership is also pretty important.

Character Set membership is a very important common additional measure.
Meaningful sequences are also a fairly common one. ( think SETI or crypto or captchas )
Additional more abstract context sets also exist.

So think carefully before assuming a string is only empty based on length or whitespace.
Reply

#12
You can easily check if string is empty with this:


if ([yourstring isEqualToString:@""]) {
// execute your action here if string is empty
}
Reply

#13
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.
Reply

#14
You can check either your string is empty or not my using this method:

+(BOOL) isEmptyString : (NSString *)string
{
if([string length] == 0 || [string isKindOfClass:[NSNull class]] ||
[string isEqualToString:@""]||[string isEqualToString:NULL] ||
string == nil)
{
return YES; //IF String Is An Empty String
}
return NO;
}

Best practice is to make a shared class say UtilityClass and ad this method so that you would be able to use this method by just calling it through out your application.
Reply

#15

I have checked an empty string using below code :


//Check if we have any search terms in the search dictionary.
if( (strMyString.text==(id) [NSNull null] || [strMyString.text length]==0
|| strMyString.text isEqual:@"")) {

[AlertView showAlert:@"Please enter a valid string"];
}

Reply

#16
Its as simple as `if([myString isEqual:@""])` or `if([myString isEqualToString:@""])`
Reply

#17
You have 2 methods to check whether the string is empty or not:

Let's suppose your string name is `NSString *strIsEmpty`.

**Method 1:**

if(strIsEmpty.length==0)
{
//String is empty
}

else
{
//String is not empty
}


**Method 2:**

if([strIsEmpty isEqualToString:@""])
{
//String is empty
}

else
{
//String is not empty
}


Choose any of the above method and get to know whether string is empty or not.

Reply

#18
Very useful post, to add NSDictionary support as well one small change


static inline BOOL isEmpty(id thing) {
return thing == nil
|| [thing isKindOfClass:[NSNull class]]
|| ([thing respondsToSelector:@selector(length)]
&& ![thing respondsToSelector:@selector(count)]
&& [(NSData *)thing length] == 0)
|| ([thing respondsToSelector:@selector(count)]
&& [thing count] == 0);
}
Reply

#19
- (BOOL)isEmpty:(NSString *)string{
if ((NSNull *) string == [NSNull null]) {
return YES;
}
if (string == nil) {
return YES;
}
if ([string length] == 0) {
return YES;
}
if ([[string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) {
return YES;
}
if([[string stringByStrippingWhitespace] isEqualToString:@""]){
return YES;
}
return NO;
}
Reply

#20
if( [txtMobile.text length] == 0 )
{
[Utility showAlertWithTitleAndMessage: AMLocalizedString(@"Invalid Mobile No",nil) message: AMLocalizedString(@"Enter valid Mobile Number",nil)];
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through