问题描述
usingSystem;classTest{staticvoidSplitPath(stringpath,outstringdir,outstringname){inti=path.Length;while(i>0){//while循环也看不懂charch=path[i–1];if(ch=='\'||ch=='/'||ch==':')break;i--;}dir=path.Substring(0,i);//这里的0是干什么的?name=path.Substring(i);}staticvoidMain(){stringdir,name;SplitPath("c:\Windows\System\hello.txt",outdir,outname);Console.WriteLine(dir);Console.WriteLine(name);}}为什么此例产生输出是:c:WindowsSystemhello.txt
解决方案
解决方案二:
1,循环是从最后一个字符开始循环检测是不是,/,:,如果是则i--,代表这个,/,:在字符串中的index2,path.Substring(0,i);0是从字符的第一个开始
解决方案三:
LZ给这样的代码,真的很少有人愿意看,太乱!!!这些基础的类库在MSDN上可以找到完整的解释。usingSystem;classTest{staticvoidSplitPath(stringpath,outstringdir,outstringname){intleftPathLength=path.Length;//pathLength是pathstring的长度,没得说while(leftPathLength>0)//余下的未解析的path串长度{charch=path[leftPathLength-1];//取出path中的leftPathLength-1处的字符if(ch=='\'||ch=='/'||ch==':')//判断这个字符是否为系统路径的分隔符,从逻辑来看,分出文件名后就结束查找break;leftPathLength--;}dir=path.Substring(0,leftPathLength);//从path的第0开始,长度为leftPathLength个字符name=path.Substring(leftPathLength);}staticvoidMain(){stringdir,name;SplitPath("c:\Windows\System\hello.txt",outdir,outname);Console.WriteLine(dir);Console.WriteLine(name);}}
解决方案四:
明白了!谢谢!!!下次发帖不会在这么乱了!呵呵