开发者

Removing a certain section of a path

I've got a number of paths like in memory (contained in an ArrayList):

C:\Program Files\Product\file.xml

What I want to do is remove the 'C:\Program Files\' from the path, so that just 'Product\file.xml'. I know I could do this simply by replacing the 'C:\Program Files\' with '', but the problem comes when I have paths from a localized environment, e.g. German where the path becomes:

C:\Programme\Product\file.xml

Any suggestions on how to do this?

In plain English, I want to remove everything before and including the se开发者_如何学JAVAcond '\'.

Thanks.


If you want to take everything after the second backslash, use this:

path.Substring(path.IndexOf('\\', path.IndexOf('\\') + 1) + 1)

If all you want to do is take everything after the second-to-last backslash, use this:

path.Substring(path.LastIndexOf('\\', path.LastIndexOf('\\') - 1) + 1)


I don't have visual studio on this machine, so I can't verify, but it should be something along these lines:

myString.Substring(myString.IndexOf('\\', myString.IndexOf('\\')))

You might need to add a "+ 1" after that second IndexOf call to get it to search at the right place, though

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜