如何根据特定字符串获取数组中的对象

时间:2012-06-18 08:58:09

标签: iphone

我是iphone的新手。我有一个包含如下对象的数组  “04_Num”,

  "04_Num/04Num.m3u",
    "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"

但是在这里我只想要包含.mp3扩展名的对象,然后我必须将这些对象放到另一个数组中

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

3 个答案:

答案 0 :(得分:0)

您可以迭代并获取只有.mp3

的那个

喜欢这样

//yourArrayThatContainsAllStrings contains all the strings
NSMutableArray *arrayOfMp3 = [[NSMutableArray alloc] init];

for (NSString *str in yourArrayThatContainsAllStrings) {
    if ([str rangeOfString:@".mp3"].location != NSNotFound) {
        [arrayOfMp3 addObject:str];
    }
}

//arrayOfMp3 will contain only the .mp3 files

答案 1 :(得分:0)

NSMutableArray *mpthrees = [[NSMutableArray alloc] initWithCapacity:0];
for (NSString *file in songs) //Where songs is the array with the paths you have provided
{
    BOOL isMpthree = [[file pathExtension] isEqualToString:@"mp3"];
    if (isMpthree) [mpthrees addObject:file];
}
// Now mpthrees array holds only paths pointing to .mp3 files

答案 2 :(得分:0)

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

for (NSString *str in stringsArray) {
    if ([str hasSuffix:@".mp3"]) {
        [filteredArray addObject:str];
    } }

//filteredArray will contain only the strings ending with ".mp3"

P.S。由于您刚刚开始使用Objective C,我想重申Objective C NSString对象始终以@符号开头。 @ “04_Num / 04Num001.mp3”