angelcake angelcake - 4 months ago 13
Vb.net Question

VB.Net Reverser and Substring a string

I have multiple files with date and each file has different length of file name.
I would like to cut the string to only get the file name. The way I do it in Sql is REVERSE(SUBSTRING(REVERSE('the full file name'), 10, LEN('the full file name')))

Example File names:

Myfile_20160708
,
MyFile123_20160708
,
Myfiles12345_20160708


And I want the result to be:

Myfile
,
MyFile123
,
Myfiles12345


How do I accomplish it in VB.Net Code?

Answer

Something like this.

Dim s As String = "Myfile_20160708"
If s.Contains("_"c) Then
    s = s.Substring(0, s.IndexOf("_"c))
End If

uses Indexof to find the location of the underscore and substring to get the part you are interested in

Comments