Substring in vb

I am trying to execute the Substring function in the image file name. The format of the name is in " images.png ".

I tried using Substring , it allows me to specify only the first character before the character "n" to execute the function.

Thus, SubString(1,6) .

But I want to get the character before . .

For example, images.png ":

After the Substring function Substring I should get " images ".

+6
source share
5 answers

You can use LastIndexOf in combination with Substring :

 myString.Substring(0, myString.LastIndexOf('.')) 

Although the Path class has a method that will do this in a strongly typed way, whether the passed path has a directory path or not:

 Path.GetFileNameWithoutExtension("images.png") 
+8
source

How about using the Path class.

 Path.GetFileNameWithoutExtension("filename.png"); 
+5
source

In general, for such string manipulations you can use:

 mystring.Split("."c)(0) 

But to get the file name without the extension, it is best to use this method:

System.IO.Path.GetFileNameWithoutExtension

+3
source
 string s = "images.png"; Console.WriteLine(s.Substring(0, s.IndexOf("."))); 
+2
source
 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

+2
source

Source: https://habr.com/ru/post/906489/


All Articles