Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
path = "/a/./b/../c/", => "/a/c"
path = "/a/./b/c/", => "/a/b/c"
"/../"?"/".‘/‘ together, such as "/home//foo/"."/home/foo".
public string SimplifyPath(string path) { List<string> list = new List<string>(); string[] words = path.Split(‘/‘); for(int i = 0; i< words.Count(); i++) { if(words[i]== "" || words[i] == ".") { ; } else if(words[i] == "..") { if(list.Count()>0) list.RemoveAt(list.Count()-1); } else list.Add(words[i]); } string res = ""; foreach( var l in list) { res += "/"+l; } if(res == "") return "/"; return res; }
71. Simplify Path QuestionEditorial Solution
原文:http://www.cnblogs.com/renyualbert/p/5860903.html