Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-07-2009, 11:45 AM   #1 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 57
Default Objective C IndexOf String

Hey, I am trying to get the INDEX of a specific STRING in a string. I went through the API and looked at rangeOfString and whatnot but that is not what I am looking for. Here is what I need...

String s="Make money with iPhone apps";
int beginIndex=s.indexOf("iPhone");

beginIndex should equal 16.

Then from there I should be able to use substringWithRange and substring from beginIndex to beginIndex+6, to get a final string of "iPhone".

This is just an example, not exactly what I need to do but I need to find this. The reason is because I have a multi thousand character string and need to find the index of specific strings in it.

Thanks
PhysX is offline   Reply With Quote
Old 07-07-2009, 12:06 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

The method rangeOfString is what you want. It returns a range, which has two fields - .length and .location.

Code:
NSString *s=@"Make money with iPhone apps";
int beginIndex= [s rangeOfString:@"iPhone].location;
__________________

Free Games!
smasher is offline   Reply With Quote
Old 07-07-2009, 04:18 PM   #3 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 57
Default

Right thanks, but something like this keeps going out of bounds for some reason. It makes no sense to me as to why...

Code:
NSString *hold=[page substringWithRange:NSMakeRange(page.length-200,page.length-150)]
Now I know it seems like it can go out of bounds from becoming negative, but it can't because I did a length check on 'page' and it came out of length 99,419. So it should just be substringing from values INSIDE that string, but it goes OOB...any idea why?

And this isn't what am actually using this for. For some reason the index to index+30 (which was around index of 66,000) was going OOB and I couldn't figure out why, so then I did this test just to see, and that's what I found. Numbers too large?

Thanks
PhysX is offline   Reply With Quote
Old 07-07-2009, 05:04 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by PhysX View Post
Code:
NSString *hold=[page substringWithRange:NSMakeRange(page.length-200,page.length-150)]
The function NSMakeRange takes a start location and a *length* , not a start and end location. In your example, if the length is 1000, then you're trying to get a string starting at 800 with length of 850. 800 + 850 = 1650, which is out of range for string with length 1000.

Try this:
Code:
NSString *hold=[page substringWithRange:NSMakeRange(page.length-200,50)]
__________________

Free Games!

Last edited by smasher; 07-08-2009 at 11:15 AM.
smasher is offline   Reply With Quote
Old 07-07-2009, 05:04 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 27
Default

This is what I used today:

Code:
NSString *string = @"blahblehbLuH";

NSRange startRange = [string rangeOfString:@"blah"];

NSRange endRange = [string rangeOfString:@"bluh"
                                 options:NSCaseInsensitiveSearch];

NSRange subStringRange = NSMakeRange(startRange.length,
                                     endRange.location - startRange.length);

NSString *subString = [string substringWithRange:subStringRange];

NSLog(@"%@", subString); // prints "bleh"
Basically, the first parameter for NSMakeRange function is the index location where the range starts, and the seconds parameter is the length of the sub string.

Colors are fun
Gurpartap Singh is offline   Reply With Quote
Old 07-07-2009, 05:11 PM   #6 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 57
Default

Ah great, thanks guys. Not used to programming in this language and I keep trying to do things the way I am used too

Edit: I can't find this for the life of me. How can you set a start index? So If I want to search for another string using rangeOfString but only from a specific index. I saw NSAnchoredSearch but you never specify the index you want to start at for that...THanks

Last edited by PhysX; 07-07-2009 at 05:41 PM.
PhysX is offline   Reply With Quote
Old 07-08-2009, 10:21 AM   #7 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 57
Default

Quote:
Originally Posted by PhysX View Post
Ah great, thanks guys. Not used to programming in this language and I keep trying to do things the way I am used too

Edit: I can't find this for the life of me. How can you set a start index? So If I want to search for another string using rangeOfString but only from a specific index. I saw NSAnchoredSearch but you never specify the index you want to start at for that...THanks
Anyone on the second part? Starting from an index.

Thanks
PhysX is offline   Reply With Quote
Old 07-08-2009, 11:18 AM   #8 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by PhysX View Post
Anyone on the second part? Starting from an index.

Thanks
There's rangeOfString: options:range: - it lets you restrict your search to a specific range.
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting 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



» Advertisements
» Online Users: 924
13 members and 911 guests
Abb4ertW, cacao, Desert Diva, Diegan, glennrichard113, ios dev, leahov, mizzytheboy, nguyenhieu129, Ocguy, pinky, qkstrk, Snail
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,298
Threads: 89,032
Posts: 379,810
Top Poster: BrianSlick (7,086)
Welcome to our newest member, glennrichard113
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:19 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0