Reputation: 1304
I am trying to perform a Substring
function on a image filename.
The name format is in "images.png".
I tried using Substring
it only allow me to indicate the first character till the "n" character to perform the function.
Such that SubString(1,6)
.
But what I want is to get any character before the .
.
For example "images.png":
After the Substring
function I should get "images".
Upvotes: 6
Views: 28249
Reputation: 2064
Dim fileName As String = "images.png"
fileName = IO.Path.GetFileNameWithoutExtension(fileName)
Debug.WriteLine(fileName)
http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx
Upvotes: 3
Reputation: 499352
You can use LastIndexOf
in conjunction with Substring
:
myString.Substring(0, myString.LastIndexOf('.'))
Though the Path
class has a method that will do this in a strongly typed manner, whether the passed in path has directories or not:
Path.GetFileNameWithoutExtension("images.png")
Upvotes: 11
Reputation: 25475
How about using the Path
class.
Path.GetFileNameWithoutExtension("filename.png");
Upvotes: 6
Reputation: 4090
In general for such string manipulations you can use:
mystring.Split("."c)(0)
But specifically for getting a filename without extension, it's best to use this method:
System.IO.Path.GetFileNameWithoutExtension
Upvotes: 3
Reputation: 33474
string s = "images.png";
Console.WriteLine(s.Substring(0, s.IndexOf(".")));
Upvotes: 2