I have a full path which I would like to remove certain levels of it. So for instance,
/home/john/smith/web/test/testing/nothing/
I would like to get rid of 4 levels, so I get
/test/testing/nothing/
What would be a good of doing this?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A simple solution is to slice the path up into parts, and then manipulate the array before sticking it back together again:
Of course, if you wanted to remove that specific path, you could also use a regular expression:
One word of advice though. If your application is juggling around with paths a lot, it may be a good idea to create a class to represent paths, so as to encapsulate the logic, rather than have a lot of string manipulations all over the place. This is especially a good idea, if you mix absolute and relative paths.