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".
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")
How about using the Path
class.
Path.GetFileNameWithoutExtension("filename.png");
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With