 |
 |
Finding out if a string contains another string
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
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
|
|
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=<font color = blue><font color = blue>0</font></font>;i<A.length-B.length+<font color = blue><font color = blue>1</font></font>;i++)
{
if(A[i]==B[<font color = blue><font color = blue>0</font></font>])
{
exists=true;
for (j=<font color = blue><font color = blue>0</font></font>,k=i;j<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
<EDIT: ubb code>
<EDIT: true!=false...duh.>
[ 04-29-2002: Message edited by: MrBS ]
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2001
Location: Seattle
Status:
Offline
|
|
Gah. that's ugly. Can't get the formatting to stay. Sorry.
~BS
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status:
Offline
|
|
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 ]
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2001
Location: Seattle
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
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
|
|
|
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|