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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Shaun VermaakTechnical SpecialistCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.