beny lim
beny lim

Reputation: 1304

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

Upvotes: 6

Views: 28249

Answers (5)

pingoo
pingoo

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

Oded
Oded

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

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25475

How about using the Path class.

Path.GetFileNameWithoutExtension("filename.png");

Upvotes: 6

Peladao
Peladao

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

shahkalpesh
shahkalpesh

Reputation: 33474

string s = "images.png";
Console.WriteLine(s.Substring(0, s.IndexOf(".")));

Upvotes: 2

Related Questions