Printing the length of a string from a label - ios

I am currently trying to check the length of a label.
What it is is i want the label to display "Unavailable" if the string is of a null value.
The sting is being read in from a XML sheet so i don't know what the length of the actual string is and i would like to know. This is what i currently have but its not working.
It displays the relevant text if its not empty which is brilliant.
But its not displaying the text when it is empty which is leading me to believe although i assume its empty its not.
Thanks in advance.
if ([l_subCommunity.text length] > 0)
{
[l_subCommunity setText:_property.str_subCommunity];
NSLog(#"%",l_subCommunity);
}
else
{
NSMutableString *sub = [[NSMutableString alloc]init];
[sub appendString:#"Unavailable"];
[self.l_subCommunity setText:sub];
}

[l_subCommunity setText:_property.str_subCommunity];
[self.l_subCommunity setText:sub];
you are using l_subCommunity setText in the if and self.l_subCommunity setText in the else. are you using 2 different variables?
Also why are you creating a mutable string to pass in the value #"Unavailable" ?
why not simply:
[self.l_subCommunity setText: #"Unavailable" ];

Your if statement is checking the wrong variable. You want:
if (_property.str_subCommunity.length) {
l_subCommunity.text = _property.str_subCommunity;
NSLog(#"%",l_subCommunity);
} else {
self.l_subCommunity.text = #"Unavailable";
}
Also keep in mind that you may end up with whitespace and/or newlines in your string as a result of parsing the XML file. You may need to trim this whitespace from your string.

Related

How to Pass Empty Values to Array?

I have Data. It has empty value and the values are string.In that some of the strings are empty.It does not have value.Now I want to pass empty string values to Array.Application is crashing if I pass empty values(null and empty) to Array.How to check and send value to Array.Can anyone help me please?
STEP 1: check string is empty or not
- (NSString *)checkEmpty:(NSString *)check
{
#try
{
if (check.length==0)
check = #" ";
if([check isEqual:[NSNull null]])
check = #" ";
}
#catch (NSException *exception)
{
check = #" ";
}
}
STEP 2:Adding the String to Array
[array addObject:[self checkEmpty:strValue]];
If the string value is empty,it takes as above coding after that it passes or adds to array.If the string has value it directly adds to the array.
You can use
array_filter();
Which manage empty array.
If I have understood your problem correctly then you are trying to insert a nil value to an array
You cannot insert a nil or a Null value to an array an empty string would be #"".
If this is not what you are looking for please be more elaborate and attach the piece of code along with your question as your question is not clear

NSString: Best way to check if string contains another string with special format?

I have a string representing fruits, separated by dot:
apple.orange.banana.watermelon
and each fruit may have a tag:
apple.orange.[old]banana.[juicy]watermelon
also there may be fruit names which are partially overlapping:
apple.orange.[old]banana.[juicy]strawberry.[fresh]berry
also the tags may be same as fruit names:
apple.orange.[old]banana.[berry]strawberry.[fresh]berry
I need to check if such a string contains a specified fruit, so given the above string and a fruit name, say "berry", I want to know the string contains "berry" and of course it should not tell me YES if "berry"'s not there but "strawberry" is.
A quick way came up in my mind is:
use componentsSeparatedByString to get an array of components (fruit names with tags)
go through each component, check if it has a tag (ie square brackets), remove it if YES
then check the remaining string is exactly the given fruit name
I cannot just use the whole string with rangeOfString because "berry" is a substring of "strawberry", I can't even first get components then check substring for each component because tags may be the same as fruit names.
I wonder is there any better way to do this? Better in terms of memory footprint and/or speed?
Thanks!
There are plenty of ways to do this.
You could use a regular expression.
You could split the string into parts.
But instead, let's notice that, when “berry” (the desired fruit) isn't the first or last fruit in the string, it's got to appear as either ]berry. or as .berry., and neither of those can match if “berry” is a substring.
So we can solve this problem quite simply putting a . on each end of the string before searching it for one of those patterns:
BOOL stringContainsFruit(NSString *string, NSString *fruit) {
string = [NSString stringWithFormat:#".%#.", string];
NSString *dotFruit = [NSString stringWithFormat:#".%#.", fruit];
if ([string rangeOfString:dotFruit].location != NSNotFound) {
return YES;
}
NSString *bracketFruit = [NSString stringWithFormat:#"]%#.", fruit];
if ([string rangeOfString:bracketFruit].location != NSNotFound) {
return YES;
}
return NO;
}

How do I search an NSString sentence with words separated by commas for a specific word in iOS7?

I came across a question that had an example using rangeOfString. How ever the first thing that came to mind was NSPredicate.
I have different strings that return words separated by sentences. For example I have one that returns "Male, Female".
What is the most efficient way to search either "Male" or "Female". I'd like to perform some actions if the word happens to be part of the sentence and if it doesn't.
NSDictionary with stored words separated by commas. I use different keys to grab specific bunch of words. Below I use the "selectedGenders" key which returns "Male, Female":
if ([combinedRefinementSelection valueForKey:#"selectedGenders"]) {
NSString *selectedGenders = [combinedRefinementSelection valueForKey:#"selectedGenders"];
// Show string in label so customer knows how their clothes items will be filtered
[[_thisController chosenGender] setText:selectedGenders];
}
I simply want to search selectedGenders and find out if Male or Female is part of the string.
As you said, rangeOfString works just fine.
NSString* sentence = #"Male, Female";
if ([sentence rangeOfString:#"Male"].location != NSNotFound)
{
NSLog(#"Male is found");
}
if ([sentence rangeOfString:#"Female"].location != NSNotFound)
{
NSLog(#"Female is found");
}

Check if Text Field NEARLY Matches Set Text? iOS

Does anyone now how I can check if a text field NEARLY matches a set text?
I know how to check if it exactly matches, but i want it to know if its even close to the set text
So if they type HELLO WORD it indicates its close but not exact match?
if (([textfield.text isEqual:#"HELLO WORLD"]))
{
NSLog(#"Correct");
} else {
NSLog(#"Incorrect");
}
This library may be of use to you. And since it's open source, you can check the source to see how it's done. :)
Use this
For Case Insensitive :
if( [textfield.text caseInsensitiveCompare:#"My Case sensitiVE"] == NSOrderedSame ) {
// strings are equal except for possibly case
}
For Case Sensitive :
if([textfield.text isEqualToString:#"My Case sensitiVE"]) {
// Case sensitive Compare
}
You can compare each index of two string and see how many difference is there. And you should define your "nearly match", it may be difference in single character or in multiple character. And decide if you should accept it or reject it.
If you like algorithm Longest Common Subsequence is a key to your goal.. :)
use
NSString caseInsensitiveCompare:
or
- (NSComparisonResult)compare:(NSString *)aString
options:(NSStringCompareOptions)mask`
NSString *string = #"HELLO WORLD I AM JACK";
if ([string rangeOfString:#"HELLO WORLD"].location == NSNotFound) {
NSLog(#"string does not contain HELLO WORLD");
} else {
NSLog(#"string contains HELLO WORLD!");
}

Objective-C - Remove last character from string

In Objective-C for iOS, how would I remove the last character of a string using a button action?
In your controller class, create an action method you will hook the button up to in Interface Builder. Inside that method you can trim your string like this:
if ([string length] > 0) {
string = [string substringToIndex:[string length] - 1];
} else {
//no characters to delete... attempting to do so will result in a crash
}
If you want a fancy way of doing this in just one line of code you could write it as:
string = [string substringToIndex:string.length-(string.length>0)];
*Explanation of fancy one-line code snippet:
If there is a character to delete (i.e. the length of the string is greater than 0)
     (string.length>0) returns 1 thus making the code return:
          string = [string substringToIndex:string.length-1];
If there is NOT a character to delete (i.e. the length of the string is NOT greater than 0)
     (string.length>0) returns 0 thus making the code return:
          string = [string substringToIndex:string.length-0];
     Which prevents crashes.
If it's an NSMutableString (which I would recommend since you're changing it dynamically), you can use:
[myString deleteCharactersInRange:NSMakeRange([myRequestString length]-1, 1)];
The solutions given here actually do not take into account multi-byte Unicode characters ("composed characters"), and could result in invalid Unicode strings.
In fact, the iOS header file which contains the declaration of substringToIndex contains the following comment:
Hint: Use with rangeOfComposedCharacterSequencesForRange: to avoid breaking up composed characters
See how to use rangeOfComposedCharacterSequenceAtIndex: to delete the last character correctly.
The documentation is your friend, NSString supports a call substringWithRange that can shorten the string that you have an return the shortened String. You cannot modify an instance of NSString it is immutable. If you have an NSMutableString is has a method called deleteCharactersInRange that can modify the string in place
...
NSRange r;
r.location = 0;
r.size = [mutable length]-1;
NSString* shorted = [stringValue substringWithRange:r];
...

Resources