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 > determining if a string contains a character

determining if a string contains a character
Thread Tools
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 10, 2002, 07:11 PM
 
I need to determine if a string contains a certain character. Right now Im doing the following which does NOT work. could someone point me in the right direction?

if ([[dict objectForKey:@"overallmaxspeeddaydays"] doesContain:@"M"]) {
[dayMon setIntValue:1];
} else {
[dayMon setIntValue:0];
}

tia
3R1C
     
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
Jul 10, 2002, 07:37 PM
 
See the headers or documentation for -[NSString rangeOfString:].
Rick Roe
icons.cx | weblog
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Jul 10, 2002, 08:10 PM
 
And if you're too lazy to read the headers...here's a nice category you can add to NSString:

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">@implementation NSString (NSStringAdditions)

- (BOOL)containsStringNSString *)aString
{
return [self rangeOfString:aString].location != NSNotFound;
}

@end</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">then just do:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">if ([[dict objectForKey:@&quot;overallmaxspeeddaydays& ;quot;] containsString:@&quot;M&quot;])
{
[dayMon setIntValue:1];
}
else
{
[dayMon setIntValue:0];
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">
     
3R1C  (op)
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 10, 2002, 08:14 PM
 
Thanx Rick, but the docs dont really help me in this instance. Does anyone have/know of any code which does this? I need to look at code to see how it works. Apples docs rarely help me (not apples fault, im just slow).

wow thanks ibson. that not only solved my problem but showed me how to use rangeOfString.

<small>[ 07-10-2002, 09:17 PM: Message edited by: 3R1C ]</small>
3R1C
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Jul 10, 2002, 08:16 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by 3R1C:
<strong>Thanx Rick, but the docs dont really help me in this instance. Does anyone have/know of any code which does this? I need to look at code to see how it works. Apples docs rarely help me (not apples fault, im just slow).</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Look above .
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Jul 10, 2002, 08:24 PM
 
If you're not familiar with categories, you would also create a header file looking like:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">@interface NSString (NSStringAdditions)
- (BOOL)containsStringNSString *)aString;
@end</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">and import that into your other source file.
     
3R1C  (op)
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 10, 2002, 08:33 PM
 
Just to show you that I want to learn this stuff and not just have you guys write me blocks of code, I used what you taught me to come up with this:

if ([[dict objectForKey:@"overallmaxspeeddaydays"] rangeOfString:@"M"].location != NSNotFound) {
[dayMon setIntValue:1];
} else {
[dayMon setIntValue:0];
}

I will now attempt to learn these 'categories'. When I get the category to work will it be faster? More efficient?
3R1C
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Jul 10, 2002, 08:56 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by 3R1C:
<strong>Just to show you that I want to learn this stuff and not just have you guys write me blocks of code, I used what you taught me to come up with this:

if ([[dict objectForKey:@"overallmaxspeeddaydays"] rangeOfString:@"M"].location != NSNotFound) {
[dayMon setIntValue:1];
} else {
[dayMon setIntValue:0];
}

I will now attempt to learn these 'categories'. When I get the category to work will it be faster? More efficient?</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Well done . A category extends a class; it is no faster, and no more efficient--unless it is a large block of code (therefore it isn't being needlessly repeated). A category provides a convenient way to add (or alter) a method to a class, so that it can be used anywhere. For example, if you used the category I posted earlier, you would be able to send *any string* a containsString: message, instead of writing out rangeOfString, location, and NSNotFound. So, you could re-write the above code block as:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">if ([[dict objectForKey:@&quot;overallmaxspeeddaydays& ;quot;] containsString:@&quot;M&quot;])
...</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">...which is far more readable. Then, somewhere else in your application (it could be the next line, or a completely different method in another source file) you could write:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">if ([@&quot;A horse is a horse, of course, of course.&quot; containsString:@&quot;frog&quot;])....</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">...which of course saves you from re-writing the rangeOfString: code block.
To provide this functionality, you would create a file named "NSString+Additions.h", which would contain:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">#import &lt;Foundation/Foundation.h&gt;

@interface NSString (NSStringAdditions)
- (BOOL)containsStringNSString *)aString;
@end</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">and then "NSString+Additions.m", containing:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">#import &quot;NSString+Additions.h&quot;

@implementation NSString (NSStringAdditions)

- (BOOL)containsStringNSString *)aString
{
return [self rangeOfString:aString].location != NSNotFound;
}

@end</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Then, at the beginning of the source file containing your other code, you would import NSString+Additions.h, meaning you could send any string a containsString: message, and it would work.

Another popular (yet simplistic) use of categories is to allow any array or any dictionary to become a table view's datasource. So, you could have something like:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">NSArray *myArray = ...;
[myTableView setDataSource:myArray];</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">and the category would do the rest. I did something like this for Synotic a while ago; search the forums if you are interested.

I hope this (rather long) explanation helps .

<small>[ 07-10-2002, 10:00 PM: Message edited by: Ibson ]</small>
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 10, 2002, 09:23 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by 3R1C:
<strong>Just to show you that I want to learn this stuff and not just have you guys write me blocks of code, I used what you taught me to come up with this:

if ([[dict objectForKey:@"overallmaxspeeddaydays"] rangeOfString:@"M"].location != NSNotFound) {
[dayMon setIntValue:1];
} else {
[dayMon setIntValue:0];
}

I will now attempt to learn these 'categories'. When I get the category to work will it be faster? More efficient?</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Well, the category will actually be insignificantly slower (since you call the -containsString: method, and then it performs the -rangeOfString: test). But it will be more convenient, because all you have to do is import the category and you can use the -containsString: method anytime you'd like to tell whether or not a string contains a substring.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Jul 13, 2002, 06:34 AM
 
Categories are explained in detail in the book "Object-Oriented Programming and the Objective-C Language", available <a href="http://developer.apple.com/techpubs/macosx/Cocoa/ObjectiveC/ObjC.pdf" target="_blank">here</a> (also available in <a href="http://catalog.vervante.com/viewProduct.cfm?item_id=607511" target="_blank">hardcopy</a>).
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 13, 2002, 09:07 AM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Angus_D:
<strong>Categories are explained in detail in the book "Object-Oriented Programming and the Objective-C Language", available <a href="http://developer.apple.com/techpubs/macosx/Cocoa/ObjectiveC/ObjC.pdf" target="_blank">here</a> (also available in <a href="http://catalog.vervante.com/viewProduct.cfm?item_id=607511" target="_blank">hardcopy</a>).</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Also available on your hard drive at /Developer/Documentation/Cocoa/ObjectiveC/ in both PDF and HTML foms, FYI.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
   
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 10:01 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