How to get the substring from a string by a delimiter?

In VB.NET ,  How to get the substring from a string delimited by "." ?
My String has something like FileName in it . Example values
      MyFile.xls
      File.WithDotInside.xlsx
      File.xlsx

   In all the above cases, I should get the file extensions only  ( Like xls or xlsx)   -  Please note I need extension only with no dots
Sam OZAsked:
Who is Participating?
 
Pallavi GodseSr. Digital Marketing ExecutiveCommented:
Please check the example below :

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Dim str As String
        Dim strArr() As String
        Dim count As Integer
        str = "vb.net split test"
        strArr = str.Split(" ")
        For count = 0 To strArr.Length - 1
            MsgBox(strArr(count))
        Next
    End Sub
End Class

Open in new window


Output:
vb.net
split
test

Open in new window


Hope this helps you.
0
 
Donald YatesCommented:
Here's how I do it in excel.
=IFERROR(RIGHT(A1,LEN(A1)-FIND("$",SUBSTITUTE(A1,".","$",LEN(A1)-LEN(SUBSTITUTE(A1,".",""))))),A1)

basically find the number of delimiters by deleting them and subtracting the length of that string from the original.
Then substitute some other delimiter ($ above) into original in the last position, find it and right string it.

It's a trainwreck I know but it works.

VB uses indexof and replace but the method is the same.
0
 
ThierrySysAdminCommented:
as an example:

    Dim s As String = "Scooby.xls"
    Dim a As Integer = 0

    For a = s.Length To 1 Step -1
      If Mid(s, a).StartsWith(".") Then
        Debug.Print(Mid(s, a + 1))

      End If
    Next
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Shaun VermaakTechnical Specialist/DeveloperCommented:
I would use Path.GetExtension method from SYSTEM.IO namespace
extension = Path.GetExtension(fileName);

Open in new window

3
 
it_saigeDeveloperCommented:
Agreed with Shaun.  Another option would be to use the FileInfo class and just pull the extension (as with System.IO.Path.GetExtension(); this works regardless of whether the file exists or not); e.g. -
Imports System.IO

Module Module1
	Sub Main()
		Dim file = "C:\SomeFolder\SomeFile.txt"
		Dim fi = New FileInfo(file)
		Console.WriteLine($"File Name: {fi.Name}{vbCrLf}File Extension: {fi.Extension}{vbCrLf}File Path: {fi.DirectoryName}{vbCrLf}File Exits: {fi.Exists}")
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output -Capture.PNGIt is also simple enough to pull the dot off by using a replace function; e.g. -
Imports System.IO

Module Module1
	Sub Main()
		Dim files = {"C:\SomeFolder\SomeFile.txt", "MyFile.xls", "File.WithDotInside.xlsx", "File.xlsx"}
		For Each file In files
			Dim fi = New FileInfo(file)
			Console.WriteLine($"File Name: {fi.Name}{vbCrLf}File Extension: {fi.Extension.Replace(".", "")}{vbCrLf}File Path: {fi.DirectoryName}{vbCrLf}File Exits: {fi.Exists}")
			Console.WriteLine()
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window

Which produces the following output -Capture.PNG
-saige-
0
 
ThierrySysAdminCommented:
There were better answers in this thread.....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.