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 > rangeOfString: in Obj-C

rangeOfString: in Obj-C
Thread Tools
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Aug 28, 2002, 10:26 AM
 
How do I implement rangeOfString: and stuff?

I need to find if a word exists in a string AND get a previous word to the match.

Heeeelp
     
Forum Regular
Join Date: Oct 2001
Location: Sweden
Status: Offline
Reply With Quote
Aug 28, 2002, 01:13 PM
 
This should work

Code:
@implementation NSString (MySearchAdditions) -(NSString*)wordBeforeString:(NSString*)searchString { NSRange searchRange, foundRange, resultRange; unsigned int precedingWsEnd, followingWsStart; foundRange = [self rangeOfString:searchString]; if ((foundRange.length == 0) || (foundRange.location == 0)) { // searchString wasn't found or it was found first in the string return @""; } // start search before the found string searchRange = NSMakeRange(0, foundRange.location); // use whitespaceAndNewlineCharacterSet as a delimiter between words foundRange = [self rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] options:NSBackwardsSearch range:searchRange]; followingWsStart = foundRange.location; // search before the whitespace searchRange = NSMakeRange(0, foundRange.location); foundRange = [self rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] options:NSBackwardsSearch range:searchRange]; if (foundRange.length == 0) { // no whitespace was found before searchString precedingWsEnd = 0; } else { precedingWsEnd = foundRange.location + foundRange.length; } resultRange = NSMakeRange(precedingWsEnd, followingWsStart - precedingWsEnd); return [self substringWithRange:resultRange]; } @end
     
VEGAN  (op)
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Aug 28, 2002, 02:00 PM
 
THANKS A MILLION!!!!!!!!!

I'M GOING TO TEST THIS OUT RIGHT AWAY
     
   
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:42 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