Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substring in vb

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".

like image 304
beny lim Avatar asked Jan 20 '12 14:01

beny lim


3 Answers

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")
like image 122
Oded Avatar answered Oct 24 '22 19:10

Oded


How about using the Path class.

Path.GetFileNameWithoutExtension("filename.png");
like image 33
Ash Burlaczenko Avatar answered Oct 24 '22 20:10

Ash Burlaczenko


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

like image 3
Peladao Avatar answered Oct 24 '22 18:10

Peladao