 |
 |
|
 |
07-07-2009, 10:45 AM
|
#1 (permalink)
|
|
Registered Member
Join Date: Nov 2008
Posts: 57
|
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
|
|
|
07-07-2009, 11:06 AM
|
#2 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,575
|
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;
__________________
|
|
|
07-07-2009, 03:18 PM
|
#3 (permalink)
|
|
Registered Member
Join Date: Nov 2008
Posts: 57
|
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
|
|
|
07-07-2009, 04:04 PM
|
#4 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,575
|
Quote:
Originally Posted by PhysX
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)]
__________________
Last edited by smasher; 07-08-2009 at 10:15 AM.
|
|
|
07-07-2009, 04:04 PM
|
#5 (permalink)
|
|
Registered Member
Join Date: Jul 2009
Posts: 18
|
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
|
|
|
07-07-2009, 04:11 PM
|
#6 (permalink)
|
|
Registered Member
Join Date: Nov 2008
Posts: 57
|
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 04:41 PM.
|
|
|
07-08-2009, 09:21 AM
|
#7 (permalink)
|
|
Registered Member
Join Date: Nov 2008
Posts: 57
|
Quote:
Originally Posted by PhysX
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
|
|
|
07-08-2009, 10:18 AM
|
#8 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,575
|
Quote:
Originally Posted by PhysX
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.
__________________
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting 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
|
|
|
|
» Advertisements |
» Online Users: 425 |
| 47 members and 378 guests |
| an5w3r_ro, andrei_c, aryaxt, ataranlen, axeman, benoitr007, bensj, Blitfast, chrish2os, CHV, Corund, Dracor, dre, drhansen, Ed99, elite, embedded, FlukeDude, gandohr, Gi-lo, GreenApple, harris, harrytheshark, HemiMG, JasonR, John_C, luks0r, Marcel, mesohorny, mmdumi, Noise, PhotoShootoutApp, Rudy, sarahconnor, schoash, shark4ever, simond, Slecorne, StefanL, StevenD, thegreyit, TomMuadib, Tonester, upperhouse, warcrow, ZunePod, _nivek |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,299
Threads: 39,096
Posts: 171,404
Top Poster: smasher (2,575)
|
| Welcome to our newest member, luks0r |
|