Removing last part of string

mig_akira

Member
Joined
Nov 19, 2009
Messages
6
Programming Experience
Beginner
Hello everyone.

I'm pretty new to VB.Net, so I need some help with a simple operation.

I have an object folder, which has an atributte name path, that contains a string with it's current path, say "folder1\folder2\folder3"

I need to remove the last ocurrence beginning on the last "\" (in this case, '\folder3'). The new string would be "folder1\folder2"

How can I do that?

Thanks!!!
 
VB.NET:
s = s.Remove(s.LastIndexOf("\"c))
's' is the variable containing the String ;)
 
When talking about folders etc specifically, Directory.GetParent Method (System.IO) would be more "elegant"
If it was a IO operation, but Name would then be "Folder2" and FullPath the absolute path rooted to current directory. It depends, but System.IO has several good helper when operating the file system.
 
Thanks again for the tip! But unfortunatly it's not a 'real' folder, it's a custom folder, made for testing purposes... so I can't use the Directory.GetParent Method (System.IO)!

Also, what if I want to do what I said but the other way around, to remove everything EXCEPT the last ocurrence after the "\"?

So if the string is "folder1\folder2\folder3" I want it to become "folder3"?

Thanks!
 
VB.NET:
s = s.Substring(s.LastIndexOf("\"c) + 1)
or
VB.NET:
s = IO.Path.GetFileName(s)
 
Back
Top