从数组中删除最后一个元素

时间:2011-10-30 14:41:50

标签: vb.net

如何在VB.NET中从数组中删除最后一个元素。我需要将街道和房屋分开。

STREET

  1. 拆分空格地址
  2. 删除最后一个元素(代码中缺少)
  3. 加入数组
  4. NUMBER

    1. 拆分空格地址
    2. 获取最后一个元素
    3. 我的代码:

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

4 个答案:

答案 0 :(得分:6)

    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)

或使用列表

    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)

就使用LINQ和性能而言,自己判断

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

答案 1 :(得分:5)

您无法从阵列中删除项目。数组的大小在您创建时决定,并且无法更改。

您可以创建一个包含数组中除最后一个项目之外的项目的结果:

Return String.Join(" ", addressArray.Take(addressArray.Length() - 1))

编辑:

如果您担心性能问题,则不应该执行任何SplitJoin,而只需使用简单的字符串操作获取字符串的部分内容:

Dim pos As Integer = args.Content.LastIndexOf(" "C)
Dim street As String = args.Content.Substring(0, pos)
Dim number As String = args.Content.Substring(pos + 1)

这比这里介绍的任何其他方法至少快十倍。

编辑2:

这是性能测试代码(C#):

Dim time As Performance = New Performance(1000000, 6)

Dim address As String = "aölskdjf öawe öofij 42"

Console.WriteLine(time.Take("SplitTakeJoin", Sub()
  Dim parts As String() = address.Split(" "c)
  Dim street As String = String.Join(" ", parts.Take(parts.Length - 1))
End Sub))
Console.WriteLine(time.Take("SplitResizeJoin", Sub()
  Dim parts As String() = address.Split(" "c)
  Array.Resize(parts, parts.Length - 1)
  Dim street As String = String.Join(" ", parts)
End Sub))
Console.WriteLine(time.Take("Substring", Sub()
  Dim street As String = address.Substring(0, address.LastIndexOf(" "c))
End Sub))

输出:

SplitTakeJoin 0,000511 ms.
SplitResizeJoin 0,000323 ms.
Substring 0,000031 ms.

使用此性能测试类:

Public Class Performance

  Private _iterations As Integer
  Private _displayDigits As Integer
  Private _emptyTime As TimeSpan

  Public Sub New(ByVal iterations As Integer, ByVal displayDigits As Integer)
    _iterations = iterations
    _displayDigits = displayDigits
    _emptyTime = TimeSpan.Zero
    _emptyTime = Take(Sub()
      End Sub)
  End Sub

  Private Function Take(ByVal action As Action) As TimeSpan
    Dim w As Stopwatch = Stopwatch.StartNew()
    For i As Integer = 0 To _iterations - 1 Step 10
      Action()
      Action()
      Action()
      Action()
      Action()
      Action()
      Action()
      Action()
      Action()
      Action()
    Next
    w.Stop()
    Return w.Elapsed - _emptyTime
  End Function

  Public Function Take(ByVal title As String, ByVal action As Action) As String
    Dim Time As TimeSpan = Take(action)
    Return title + " " + (Time.TotalMilliseconds / Convert.ToDouble(_iterations)).ToString("N" + _displayDigits.ToString()) + " ms."
  End Function

End Class

答案 2 :(得分:5)

您也可以尝试使用Redim Preserve。这些语句是从Visual Basic 6.0开始的,而不是被认为是.NET的做事方式,但它是另一种选择。根据此article,可能存在性能问题。

Redim Preserve addressArray(addressArray.Length - 1)

实际上要删除最后一项数组:

 Redim Preserve addressArray(addressArray.Length - 2)

因为它们基于零,长度是一个基础。

答案 3 :(得分:1)

这将返回字符串中除最后一个“字/数字”之外的所有字符串作为字符串数组。

Return args.content.Split(" ").Take((args.content.Split(" ").Count - 1)).ToArray()