2009-11-16 56 views
9

我想從文本中修剪文件擴展名我在表格單元格中有一個NSMutableArray修剪文件擴展名UITableView

NSMutableArray *theFiles = [NSMutableArray new]; 
NSFileManager *manager = [NSFileManager defaultManager]; 
NSArray *fileList = [manager directoryContentsAtPath:@"/Test"]; 
for (NSString *s in fileList){ 
    [theFiles addObject:fileList]; 
} 
cell.textLabel.text = theFiles[indexPath.row]; 
return cell; 

此列出例如「Xylophone.m4r」我想刪除.m4r

+1

好,我有幸得到的最好的方式是根本不使用擴展名爲我的文件。只要我使用XML方法創建文件,它仍然會讀取文件,即使沒有擴展名在那裏! – WrightsCS 2009-12-03 05:37:46

回答

38

嘗試-[NSString stringByDeletingPathExtension](在NSPathUtilities.h中)。

+0

其對於NSMutableArray – WrightsCS 2009-11-16 06:45:02

+1

您已經遍歷數組,只需執行類似[theFiles addObject:[s stringByDeletingPathExtension]]。 – 2009-11-16 06:48:35

3

其實,對於我的使用,我能夠以編程方式創建一個plist,而不是使用擴展名,它效果很好!然而,anothe rway要做到這一點:

[string stringByReplacingOccurrencesOfString:@".fileextension" withString:@""]; 
0

即使這個問題是舊的,你可以使用子:

NSString *yourFileName = @"Xylophone.m4r"; 
yourFileName = [yourFileName substringToIndex:yourFileName.length - 4]; 
0

只需要刪除路徑擴展組件:

cell.textLabel.text = [[theFiles objectAtIndex:indexPath.row] stringByDeletingPathExtension];