代码改变世界

object-c中NSString 路径操作

2013-04-23 20:29  甘超波  阅读(2159)  评论(0编辑  收藏  举报

如果您对object_c语法不是很了解或者不感兴趣,请直接跳过。

下面主要是介绍NSString 对字符串路径的操作,废话不多说直接上代码

void Path(){
    NSArray *arry=[[[NSArray alloc] initWithObjects:@"abc",@"mike",@"gan", nil] autorelease ];
    NSString *str=  [NSString pathWithComponents:arry];//将NSArray集合拼成一个路径
    NSLog(@"%@",str);
    NSString *str1=@"abc/dc/ed.txt";
    NSLog(@"%@",[str1 pathComponents]);//把路径转换成NSSArray集合。
    
    NSLog(@"%i",[str1 isAbsolutePath]);//是否是绝对路径用/开头判断
    NSLog(@"%@",[str1 lastPathComponent]);//获得最后一个文件
    NSLog(@"%@",[str1  stringByDeletingLastPathComponent]);//删除最后一个目录。
    NSLog(@"%@",[str1 stringByAppendingFormat:@".txt.%i",12]);//追加文件名
    
}

int main(int argc, const char * argv[])
{
    @autoreleasepool {        
        Path();
 
    }
    return 0;
}

 其中结果:

2013-04-23 05:32:38.130 字符串03[1105:303] abc/mike/gan
2013-04-23 05:32:38.136 字符串03[1105:303] (
    abc,
    dc,
    "ed.txt"
)
2013-04-23 05:32:38.140 字符串03[1105:303] 0
2013-04-23 05:32:38.141 字符串03[1105:303] ed.txt
2013-04-23 05:32:38.143 字符串03[1105:303] abc/dc
2013-04-23 05:32:38.144 字符串03[1105:303] abc/dc/ed.txt.txt.12