1

I have an array and the contents look something like this:

    "user/01453303276519456080/state/com.google/starred",
    "user/01453303276519456080/state/com.google/broadcast",
    "user/01453303276519456080/label/comics",
    "user/01453303276519456080/label/General News",
    "user/01453303276519456080/label/iPhone Related News",
    "user/01453303276519456080/label/Programming",
    "user/01453303276519456080/label/Sports",
    "user/01453303276519456080/label/Tech News",
    "user/01453303276519456080/label/Tutorials",
    "user/01453303276519456080/state/com.blogger/blogger-following"

I want to extract only the last words after "/" (e.g. General News, Programming, etc) The unique id after user/ will not be constant.

Can someone give me an idea to implement this?

Cheers, iSEE

iSee
  • 604
  • 1
  • 14
  • 31

4 Answers4

3

You can use lastPathComponent API of NSString over each strings of the array.

YPK
  • 1,851
  • 18
  • 18
  • Thanks for the lastPathComponent API solved it. Since you answered it before Sven, I mark this the correct answer.... – iSee Jan 18 '11 at 10:25
2

Look at the lastPathComponent method of NSString. This should give you exactly what you want. If you need the other parts too you can use the method componentsSeperatedByString: or use a NSScanner.

Sven
  • 22,475
  • 4
  • 52
  • 71
  • If you need to break up the string by the / delimiter, see http://stackoverflow.com/questions/259956/nsstring-tokenize-in-objective-c which has a good, quick example. – phaxian Jan 18 '11 at 10:25
2

use this

for(int i=0;i<[yourArray count];i++)
{
  NSMutableArray *tempArray=[[[NSMutableArray alloc] init] autorelease];
  string=[yourArray objectAtIndex:i];       
  tempArray=[[string componentsSeparatedByString:@"/"] mutableCopy];
  //use tempArray

}

now tempArray having all strings at different indexes, fetch them according to you

Ishu
  • 12,797
  • 5
  • 35
  • 51
  • no need to allocate a mutable array object, and then throw it away. the object you allocated is never used again – user102008 Feb 01 '11 at 05:43
  • @user102008, i am making NSMutableArray because you cant call objectAtIndex and other methods on NSArray. – Ishu Feb 01 '11 at 05:59
-1
for(int i=0;i<[myArray count];i++)
{
  NSMutableArray *tempArray=[[[NSMutableArray alloc] init] autorelease];
  string=[myArray objectAtIndex:i];
  NSArray *arr=[string componentsSeparatedByString:@"/"];
  tempArray=[arr objectAtIndex:([arr count]-1)];

}

Now this tempArray contains the last components as u want

Hope it will help u!

Sudhanshu
  • 3,950
  • 1
  • 19
  • 18