PDA

View Full Version : How can find a string in NSString?




newformac
Jun 9, 2011, 07:45 AM
Hi all,

please tell me how can i find the NSString variable contains given string or not?

and it should not be case sensitive.

like "Ani" find in ani, ANI, Ani, aNi , anicol ,ANIMAL etc.

please help me for this thanks in advance.



jiminaus
Jun 9, 2011, 07:51 AM
OMG, are you serious !!!???? WTF. Look in the NSString class reference. Particularly look under Finding Characters and Substrings.

newformac
Jun 9, 2011, 07:57 AM
this is correct way


BOOL match = ([main_str rangeOfString:find_str options:NSCaseInsensitiveSearch].location != NSNotFound);

lee1210
Jun 9, 2011, 07:58 AM
As jiminaus said... the documentation is your friend:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

The appropriate method is rangeOfString:options: with NSCaseInsensitiveSearch as your option mask.

It sounds like, based on your posts, that you are trying to write a program without having learned the language/frameworks. You might want to start here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html%23//apple_ref/doc/uid/TP30001163

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/Introduction/Introduction.html%23//apple_ref/doc/uid/TP40002974

-Lee

PatrickCocoa
Jun 9, 2011, 05:02 PM
this is correct way


BOOL match = ([main_str rangeOfString:find_str options:NSCaseInsensitiveSearch].location != NSNotFound);



I'm assuming "main_str" is an NSString you've created somewhere else. In which case you should make your variable names more useful (and match the Cocoa development guidelines). Something like "nameOfBaby" or "typeOfBuilding".

Also "match" should be "nicknameFoundInNameOfBaby" or "subtypeFoundInTypeOfBuilding" or something like that.

LearnerSixMonthsFromNow will thank Learner.

newformac
Jun 9, 2011, 11:40 PM
thanks for the info and guidance.