IOS caseInsensitiveCompare - ios

I have the code below that compares two values and returns if match is found, the problem is that it is case-sensitive, I googled around and found the method caseInsensitiveCompare, please help me run this program with caseInsensitiveCompare method, I am lost.
NSString *listOfnames = #"person, person1, person2, person3";
NSString *name = #"person2";
NSRange match = [listOfnames rangeOfString:name];
if(match.location == NSNotFound ){
NSLog(#"Person not found!");
}else{
NSLog(#"Found you");
}

Use the rangeOfString:options: rendition with the NSCaseInsensitiveSearch option:
NSString *listOfnames = #"person, person1, person2, person3";
NSString *name = #"PERSON2";
NSRange match = [listOfnames rangeOfString:name options:NSCaseInsensitiveSearch];
if(match.location == NSNotFound ){
NSLog(#"Person not found!");
}else{
NSLog(#"Found you");
}

Related

ABRecordCopyCompositeName and CFBridgingRelease crash issue

I am developing IOS application using AddressBook.
Here is my code what I used.
I am getting crash issue on substringWithRange function.
What is the crash reason?
Thank you.
NSString * sort_name = CFBridgingRelease(ABRecordCopyCompositeName(person));
if (sort_name != nil) {
[self Make_Sorting_Name:sort_name];
- (NSDictionary *)Make_Sorting_Name:(NSString *)sort_name {
NSString * sort_char = [[NSString stringWithString:[sort_name substringWithRange:NSMakeRange(0, 1)]] uppercaseString];
NSCharacterSet *nonDigits = [NSCharacterSet letterCharacterSet];
BOOL containsNonDigitChars = ([sort_char rangeOfCharacterFromSet:nonDigits].location == NSNotFound);
}
The ABRecordCopyCompositeName function might return nil or empty string sometimes. So the case needs to be checked:
NSString *sort_char = #""; //or another specific character for sorting
if (sort_name != nil && sort_name.length > 0){
sort_char = [[NSString stringWithString:[sort_name substringWithRange:NSMakeRange(0, 1)]] uppercaseString];
}

How to parse value from a url in iOS?

I have a url :
DBSession url : db-qf8erkjgfnhno://1/connect?oauth_token_secret=7ek3fhnfnhla&state=58444830-7764-4DBE-A4F1-84B8604sv2C5&oauth_token=c93dvvaa5b47&uid=2841089
I want to take the value of oauth_token_secret and oauth_token. I tried with [url query] method and get oauth_token_secret=7ek3fhnfnhla&state=58444830-7764-4DBE-A4F1-84B8604sv2C5&oauth_token=c93dvvaa5b47&uid=2841089, but i can not find the method to get the value of oauth_token_secret and oauth_token.
Please help me out.
You can parse query parameters into NSMutableDictionary, and after that retrieve values from there.
NSString *query = [url query];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *param in [query componentsSeparatedByString:#"&"]) {
NSArray *elts = [param componentsSeparatedByString:#"="];
if([elts count] < 2) continue;
[params setObject:[elts objectAtIndex:1] forKey:[elts objectAtIndex:0]];
}
NSLog(#"oauth_token_secret : %#", params[#"oauth_token_secret"]);
NSLog(#"oauth_token : %#", params[#"oauth_token"]);
This is a function that returns string between two characters. This is return you oath token. Change it according to your need.
-(NSString*)contentsInStrings:(NSString *)str
{
NSString *subString = nil;
NSRange range1 = [str rangeOfString:#"&oauth_token="];
NSRange range2 = [str rangeOfString:#"&"];
if ((range1.length == 1) && (range2.length == 1) && (range2.location > range1.location))
{
NSRange range3;
range3.location = range1.location+1;
range3.length = (range2.location - range1.location)-1;
subString = [str substringWithRange:range3];
}
return subString;
}

Split NSString from first whitespace

I have a name textfield in my app, where both the firstname maybe a middle and a lastname is written. Now I want to split these components by the first whitespace, the space between the firstname and the middlename/lastname, so I can put it into my model.
For example:
Textfield Text: John D. Sowers
String 1: John
String 2: D. Sowers.
I have tried using [[self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] firstObject]; & [[self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] lastObject];
But these only work if have a name without a middlename. Since it gets the first and the last object, and the middlename is ignored.
So how would I manage to accomplish what I want?
/*fullNameString is an NSString*/
NSRange rangeOfSpace = [fullNameString rangeOfString:#" "];
NSString *first = rangeOfSpace.location == NSNotFound ? fullNameString : [fullNameString substringToIndex:rangeOfSpace.location];
NSString *last = rangeOfSpace.location == NSNotFound ? nil :[fullNameString substringFromIndex:rangeOfSpace.location + 1];
...the conditional assignment (rangeOfSpace.location == NSNotFound ? <<default value>> : <<real first/last name>>) protects against an index out of bounds error.
Well that method is giving you an array with all the words split by white space, so then you can grab the first object as the first name and the rest of the objects as middle/last/etc
NSArray *ar = [self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *firstName = [ar firstObject];
NSMutableString *rest = [[NSMutableString alloc] init];
for(int i = 1; i < ar.count; i++)
{
[rest appendString:[ar objectAtIndex:i]];
[rest appendString:#" "];
}
//now first name has the first name
//rest has the rest
There might be easier way to do this, but this is one way..
Hope it helps
Daniel
I think this example below I did, solves your problem.
Remember you can assign values from the array directly, without transforming into string.
Here is an example:
NSString *textField = #"John D. Sowers";
NSArray *fullName = [textField componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" "]];
if (fullName.count)
{
if (fullName.count > 2)
{
NSLog(#"Array has more than 2 objects");
NSString *name = fullName[0];
NSLog(#"Name:%#",name);
NSString *middleName = fullName[1];
NSLog(#"Middle Name:%#",middleName);
NSString *lastName = fullName[2];
NSLog(#"Last Name:%#",lastName);
}
else if(fullName.count == 2)
{
NSLog(#"Array has 2 objects");
NSString *name = fullName[0];
NSLog(#"Name:%#",name);
NSString *lastName = fullName[1];
NSLog(#"Last Name:%#",lastName);
}
else
{
NSString *name = fullName[0];
}
}
I found this to be most robust:
NSString *fullNameString = #"\n Barnaby Marmaduke \n \n Aloysius ";
NSMutableArray *nameArray = [[fullNameString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy];
[nameArray removeObject:#""];
NSString *firstName = [nameArray firstObject];
if(nameArray.count)
{
[nameArray removeObjectAtIndex:0];
}
NSString *nameRemainder = [nameArray componentsJoinedByString:#" "];
Bob's your uncle.

NSSet to string separaing by comma

I have the next code for converting NSSet to string separating by comma:
-(NSString *)toStringSeparatingByComma
{
NSMutableString *resultString = [NSMutableString new];
NSEnumerator *enumerator = [self objectEnumerator];
NSString* value;
while ((value = [enumerator nextObject])) {
[resultString appendFormat:[NSString stringWithFormat:#" %# ,",value]];//1
}
NSRange lastComma = [resultString rangeOfString:#"," options:NSBackwardsSearch];
if(lastComma.location != NSNotFound) {
resultString = [resultString stringByReplacingCharactersInRange:lastComma //2
withString: #""];
}
return resultString;
}
It seems that it works, but I get here two warnings:
1. format string is not a string literal (potentially insecure)
2. incompatible pointer types assigning to nsmutablestring from nsstring
How to rewrite it to avoid of warnings?
There is another way to achieve what you are trying to do with fewer lines of code:
You can get an array of NSSet objects using:
NSArray *myArray = [mySet allObjects];
You can convert the array to a string:
NSString *myStr = [myArray componentsJoinedByString:#","];
stringByReplacingCharactersInRange method's return type NSString. You are assigning it to NSMutableString. Use mutable copy.
resultString = [[resultString stringByReplacingCharactersInRange:lastComma //2
withString: #""]mutablecopy]

How to compare two ID

I have two Ids
85816465-FA7B-48B1-8AD3-7FB0A1B6C011 - 85816465-fa7b-48b1-8ad3-7fb0a1b6c011
As you can see, they are almost the same, but there is difference )
85816465-FA7B-48B1-8AD3-7FB0A1B6C011 this code i'm compile by this code
CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
NSString * uuidString = (__bridge NSString*)CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
CFRelease(newUniqueId);
after this insert it into database (Postgres) and database converts it onto this
85816465-fa7b-48b1-8ad3-7fb0a1b6c011
When i'm selecting this inserted Id and trying to compare it with old, Xcode gives me that they are not equal ...
any suggestions?
when you are comparing the strings convert them to Uppercase if that is the only diffrence using the method
uuidString=[uuidString uppercaseString];
Please try to use this one ...I hope it may help you
NSString *str1 = #"85816465-FA7B-48B1-8AD3-7FB0A1B6C011";
NSString *str2 = #"85816465-fa7b-48b1-8ad3-7fb0a1b6c011";
str1 = [str1 stringByReplacingOccurrencesOfString:#"-" withString:#""];
str2 = [str2 stringByReplacingOccurrencesOfString:#"-" withString:#""];
if( [str1 caseInsensitiveCompare:str2] == NSOrderedSame )
NSLog(#"ITS EQUAL");
else
NSLog(#"ITS NOT EQUAL");
Try this
NSRange r = [udidstring1 rangeOfString:udidstring2 options:NSCaseInsensitiveSearch];
if(r.location != NSNotFound)
{
NSLog(#"Both UDID are same");
}
or you can try this
if ([udidstring1 caseInsensitiveCompare:udidstring2]==NSOrderedSame) {
NSLog(#"Both UDID are same");
}

Resources