0

I have a file name like below:

sub_fa__hotchkis_type1a__180310__PUO4x4__180813

I want to separate it with double underscores "__" and using this code:

        Dim MdlNameArr() As String = Path.GetFileNameWithoutExtension(strProjMdlName).Split(New Char() {"__"}, StringSplitOptions.RemoveEmptyEntries)
        myTool.Label9.Text = MdlNameArr(1).ToString

I expect the result will be "hotchkis_type1a" but it returns "fa".

It doesnt recognize single underscore "_".

Is there any method to use it properly?

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • 2
    You need to use the [String.Split(string(), options)](https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=netframework-4.7.2#System_String_Split_System_String___System_StringSplitOptions_) overload. – Andrew Morton Sep 22 '18 at 22:28
  • 3
    If you use [`Option Strict On`](https://stackoverflow.com/q/5160669/1115360) it will point out problems like that for you. – Andrew Morton Sep 22 '18 at 22:50
  • 1
    @AndrewMorton can you show us how? I read the documentation a couple of times and i searched on google but i didn't find how the options could help – Simo Sep 25 '18 at 15:32
  • 1
    @AndrewMorton you deserve an upvote but I used all my votes today :c – Simo Sep 25 '18 at 15:54
  • 1
    I will tomorrow ;) – Simo Sep 25 '18 at 18:30

1 Answers1

1

You need to split on a string rather than just a character, so if we look at the available overloads for String.Split, we find the nearest one to that is String.Split(string(), options) which takes an array of strings as the separators and requires the inclusion of StringSplitOptions like this:

Dim s = "sub_fa__hotchkis_type1a__180310__PUO4x4__180813"
Dim separators() As String = {"__"}
Dim parts = s.Split(separators, StringSplitOptions.None)

If parts.Length >= 2 Then
    Console.WriteLine(parts(1))
Else
    Console.WriteLine("Not enough parts found.")
End If

Outputs:

hotchkis_type1a

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84