Search code examples
.netvb.netsplitautocadtake

How to get the first n elements of string sequence with split.take? VB .net


I created a .dll plugin for both AutoCAD and CIVIL 3D.

I'm trying to retrieve the first elements of these sequences:

"Autodesk AutoCAD 2019 - [Drawing 1]" I just want to obtain "Autodesk AutoCAD 2019" (I want first 3 elements) and "Autodesk CIVIL 3D 2019 - [Drawing 1]" I just want to obtain "Autodesk CIVIL 3D 2019". (I want first 4 elements)

I obtain these sequences by using Process.GetCurrentProcess().MainWindowTitle which is retrieving the window title of the application I'm using at the moment.

However, sometimes I'm using AutoCAD, others I'm using CIVIL 3D and I want to retrieve their application names as strings, displayed in a message box, telling me which application I'm using at the moment.

I have tried with:

Process.GetCurrentProcess().MainWindowTitle.Substring(0, 22)

but this one is not reliable for when I display the message box at the start of autocad or when closing. It only works if I display the message box in between start and closing.

I think I should try with:

Process.GetCurrentProcess().MainWindowTitle.Split.Take(4)

or

Process.GetCurrentProcess().MainWindowTitle.Take(4)

but I can't seem to get it to work despite already trying to understand it (https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/linq/return-or-skip-elements-in-a-...).

How should I do it with Take???

Many Thanks!


Solution

  • I would do:

    Dim originalString As String = Process.GetCurrentProcess().MainWindowTitle
    
    Dim splittedString As String() = originalString.Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
    
    Dim productName As String = splittedString(0)