Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove last element from array

Tags:

vb.net

How to remove the last element from an array in VB.NET. I need to split the street and housenumber.

STREET

  1. Split the address on spaces
  2. Remove last element (missing in the code)
  3. Join array

NUMBER

  1. Split the address on spaces
  2. get last element

My code:

'split address
Dim addressArray() As String = args.Content.Split(" ")

'remove last element and return the joined array
Return String.Join(" ", addressArray.Remove(addressArray.Length() - 1))
like image 811
Creep Avatar asked Dec 03 '22 06:12

Creep


1 Answers

    Dim foo() As String = "This is a test".Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
    Array.Resize(foo, foo.Length - 1)
    Dim s As String = String.Join(" ", foo)

or use lists

    Dim foo As New List(Of String)
    foo.AddRange("This is a test".Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries))
    foo.RemoveAt(foo.Count - 1)
    Dim s As String = String.Join(" ", foo)

As far as using LINQ and performance, judge for yourself

Public Class Form1

    'to LINQ or not to LINQ
    'judge for yourself

    Dim stpw As New Stopwatch

    Private Sub Button1_Click(sender As System.Object, _
                              e As System.EventArgs) Handles Button1.Click

        Dim ipsumA() As String = New String() {"Lorem", "ipsum", "dolor", "sit", _
                                               "amet", "consectetur", "adipisicing", _
                                               "elit", "sed", "do", "eiusmod", _
                                               "tempor", "incididunt", "ut", "labore", _
                                               "et", "dolore", "magna", "aliqua", "Ut", _
                                               "enim", "ad", "minim", "veniam", "quis", _
                                               "nostrud", "exercitation", "ullamco", _
                                               "laboris", "nisi", "ut", "aliquip", "ex", _
                                               "ea", "commodo", "consequat", "Duis", "aute", _
                                               "irure", "dolor", "in", "reprehenderit", "in", _
                                               "voluptate", "velit", "esse", "cillum", "dolore", _
                                               "eu", "fugiat", "nulla", "pariatur", "Excepteur", _
                                               "sint", "occaecat", "cupidatat", "non", "proident", _
                                               "sunt", "in", "culpa", "qui", "officia", "deserunt", _
                                               "mollit", "anim", "id", "est", "laborum"}

        Const tries As Integer = 100000
        Debug.WriteLine("")

        stpw.Reset()
        stpw.Start()
        For x As Integer = 1 To tries
            Dim s As String = arrayTake(ipsumA)
        Next
        stpw.Stop()
        Debug.WriteLine(stpw.ElapsedTicks.ToString)

        stpw.Reset()
        stpw.Start()
        For x As Integer = 1 To tries
            Dim s As String = arrayRsz(ipsumA)
        Next
        stpw.Stop()
        Debug.WriteLine(stpw.ElapsedTicks.ToString)

    End Sub

    Private Function arrayRsz(test As String()) As String
        Array.Resize(test, test.Length - 1)
        Return String.Join(" ", test)
    End Function

    Private Function arrayTake(test As String()) As String
        Return String.Join(" ", test.Take(test.Length - 1))
    End Function
End Class
like image 121
dbasnett Avatar answered Dec 31 '22 06:12

dbasnett