检索数组中字符串的子字符串

时间:2012-06-18 09:24:26

标签: iphone

我是iphone的新手。我有一个包含下面这些对象的数组

"04_Num/04Num001.mp3",
"04_Num/04Num002.mp3",
"04_Num/04Num003.mp3",
"04_Num/04Num004.mp3",
"04_Num/04Num005.mp3",
"04_Num/04Num006.mp3",
"04_Num/04Num007.mp3",
"04_Num/04Num008.mp3",
"04_Num/04Num009.mp3",
"04_Num/04Num010.mp3",
"04_Num/04Num011.mp3",
"04_Num/04Num012.mp3",
"04_Num/04Num013.mp3",
"04_Num/04Num014.mp3",
"04_Num/04Num015.mp3",
"04_Num/04Num016.mp3",
"04_Num/04Num017.mp3",
"04_Num/04Num018.mp3",
"04_Num/04Num019.mp3",
"04_Num/04Num020.mp3",
"04_Num/04Num021.mp3",
"04_Num/04Num022.mp3",
"04_Num/04Num023.mp3",
"04_Num/04Num024.mp3",
"04_Num/04Num025.mp3",
"04_Num/04Num026.mp3",
"04_Num/04Num027.mp3",
"04_Num/04Num028.mp3",
"04_Num/04Num029.mp3",
"04_Num/04Num030.mp3",
"04_Num/04Num031.mp3",
"04_Num/04Num032.mp3",
"04_Num/04Num033.mp3",
"04_Num/04Num034.mp3",
"04_Num/04Num035.mp3",
"04_Num/04Num036.mp3"

但在这里我只想在/(即)之后检索字符串(对象),例如04_Num / 04Num033.mp3在这里我只想要字符串04Num033.mp3.Like这对于以上所有,然后我必须放置在一个数组

如果有任何人知道这一点,请帮助我......

2 个答案:

答案 0 :(得分:2)

lastPathComponent就是您所需要的。你可以这样做:

NSMutableArray *files = [[NSMutableArray alloc] initWithCapacity:0];
for (NSString *file in songs) //Where songs is the array with the paths you have provided
{
    [files addObject:[file lastPathComponent]];
}

答案 1 :(得分:-1)

您可以使用NSString的

将字符串分成两部分
  

componentsSeparatedByString:

方法,并使用最后一个字符串组件

// Let's call your array of strings as stringsArray
NSMutableArray *prefixStrings = [[NSMutableArray alloc] init];

for (NSString *str in stringsArray) {
    NSArray *stringComponents = [str componentsSeparatedByString:@"/"];
    if ([stringComponents count]) {
        [prefixStrings addObject:[stringComponents objectAtIndex:1]];
    } }