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 out if a string contains another string

Finding out if a string contains another string
Thread Tools
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Apr 27, 2002, 02:25 AM
 
I'm implementing a search function in my Cocoa (Obj-C) app, and need to find code that returns whether or not string A contains string B. For some other strings, I also need to find out whether or not string A contains any of the words in string B.
Thanks!
     
Grizzled Veteran
Join Date: Jun 2001
Location: Seattle
Status: Offline
Reply With Quote
Apr 27, 2002, 03:37 AM
 
Not sure about the Obj C syntax, but pseudocode is as follows
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
bool containsSubString(string A, string B)
{
int i,j,k;
bool exists=false;

for(i=&lt;font color = blue&gt;<font color = blue>0</font>&lt;/font&gt;;i&lt;A.length-B.length+&lt;font color = blue&gt;<font color = blue>1</font>&lt;/font&gt;;i++)
{
if(A[i]==B[&lt;font color = blue&gt;<font color = blue>0</font>&lt;/font&gt;])
{
exists=true;
for (j=&lt;font color = blue&gt;<font color = blue>0</font>&lt;/font&gt;,k=i;j&lt;B.length;k++,j++)
{
if(A[k]!=B[j])
{
exists=false;
j=B.length;
}
}
}
if(exists)
return true;
}
return exists;
}
</font>[/code]
Haven't slept for a while (cramming for midterms is bad, m'kay) but that should work.
~BS

&lt;EDIT: ubb code&gt;
&lt;EDIT: true!=false...duh.&gt;

[ 04-29-2002: Message edited by: MrBS ]
     
Grizzled Veteran
Join Date: Jun 2001
Location: Seattle
Status: Offline
Reply With Quote
Apr 27, 2002, 03:39 AM
 
Gah. that's ugly. Can't get the formatting to stay. Sorry.
~BS
     
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
Apr 27, 2002, 03:42 AM
 
The answer to the first question is simple. See the NSString docs for <font face = "courier">-rangeOfString:</font> (and <font face = "courier">-rangeOfString :options:</font> and <font face = "courier">-rangeOfString :options:range:</font>). If the substring you're looking for isn't found, you'll get back an NSRange whose location element is NSNotFound.

For finding out if string A contains any words from string B, you'll basically need to split string B into words (by breaking on spaces or other word boundaries, which can be specific to different human languages and writing systems), perform separate searches for each word, and OR all the results together.

By the way, if you're looking for an easy-to-implement Find/Replace panel for your app, check out our open-source OmniAppKit framework. (end shameless plug... )

[ 04-27-2002: Message edited by: Rickster ]
Rick Roe
icons.cx | weblog
     
Grizzled Veteran
Join Date: Jun 2001
Location: Seattle
Status: Offline
Reply With Quote
Apr 27, 2002, 03:49 AM
 
Rick,
Can't access the code...
http://www.omnigroup.com/ftp/pub/software/Source/
________________
Not Found

The requested URL /ftp/pub/software/Source/ was not found on this server.
     
Ibson  (op)
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Apr 27, 2002, 03:59 AM
 
Thanks Rick! I'd completely missed rangeOfString:options: in the documentation.

[ 04-27-2002: Message edited by: Ibson ]
     
Forum Regular
Join Date: Oct 2001
Location: Sweden
Status: Offline
Reply With Quote
Apr 29, 2002, 03:23 AM
 
Originally posted by MrBS:
<STRONG>Rick,
Can't access the code...
http://www.omnigroup.com/ftp/pub/software/Source/
________________
Not Found

The requested URL /ftp/pub/software/Source/ was not found on this server.</STRONG>
Try http://www.omnigroup.com/ftp/pub/sof...MacOSX/Source/

When you get an "URL not found" error, try deleting the last part of the url and try loading that. Usually it's not that hard to find the correct path that way.

/Tobias
     
   
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 11:13 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