Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Finding a string within a string...

Finding a string within a string...
Thread Tools
Junior Member
Join Date: Jul 2002
Location: Australia
Status: Offline
Reply With Quote
Sep 2, 2002, 03:22 AM
 
Is there anway in cocoa (Obj-C) to have a NSString (Bam) and find an occurance of "BAM" in Bam.

sorta like:

Code:
NSString *Bam = [[NSString alloc] init]; Bam = @"abc def ghi"; if (Bam contains @"def") { NSLog(@"Bam contains def"); }
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Sep 2, 2002, 03:35 AM
 
Please, search the archives. This has been asked a few times. Anyway, here's what to do:
Code:
NSString *bam; bam = @"abc def ghi"; if ([bam rangeOfString:@"def"].location != NSNotFound) { NSLog(@"bam contains def"); }
Note a few things: variables should always begin with a lowercase letter. Also, to create a constant string, you don't need to call alloc and init. If you do need to send alloc and init to a class, remember to send it an autorelease or release message, otherwise it will leak memory.

I suggest that you read a good "Beginner's Cocoa" book, or even just the tutorials provided by Apple.
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Sep 2, 2002, 03:35 AM
 
More precisely, about the constant string:
First you're creating an NSString object and pointing your variable to it. Then you're creating an NXConstantString object and pointing your variable to that. Meanwhile, your original string (which you created with [[NSString alloc] init]) is left floating out in your program's memory forever. And you'll wind up with more orphaned strings every time this method runs.

Honestly, I can't think of a good reason to ever use [[NSString alloc] init]. Since it's not mutable, you've just got a blank string that's good for nothing except being destroyed.
(Last edited by Chuckit; Sep 2, 2002 at 03:46 AM. )
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Syphor  (op)
Junior Member
Join Date: Jul 2002
Location: Australia
Status: Offline
Reply With Quote
Sep 2, 2002, 04:05 AM
 
Thanks for that..

and yeah sorry I will check the archives next time
     
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
Sep 2, 2002, 04:11 PM
 
Note a few things: variables should always begin with a lowercase letter.
To elabotate: obviously, you don't have to do it this way, but it's one of several common conventions followed by Cocoa programmers to make working with each other's code easier. To list a few:
  • Instance variables, local variables within methods, and method signatures should start with a lowercase letter.
  • Class (static) variables should start with an uppercase letter.
  • Class names, category names, and exported variables and constants (such as notification names) should be prefixed with two or three uppercase letters that are shared across the project. (e.g. OAPreferencesController in the OmniAppKit framework, OWBookmark in the OmniWeb application)
Rick Roe
icons.cx | weblog
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:43 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2