获取特定字符串的索引以及vb.net中的字符串?

时间:2015-06-05 16:36:05

标签: arrays vb.net

Dim str() as string = "1,2,1,2"

从上面的字符串数组我想知道重复字符串的索引,如" 1"是指数为0.2和" 2"在1,3。如何在vb.net中执行此操作?

请记住,我将从数据库中获取字符串数组。所以我不会出现超过2次的数字。

1 个答案:

答案 0 :(得分:3)

从数据库中获取字符串之后,您需要Split字符串,因此您将拥有一个数组。

然后,您将想知道数组中的所有Distinct值,以便您可以搜索并查找索引。

Imports System

Module Module1
    Sub Main()
        Dim str() As String = "1,2,1,2,3,1,0,1,4".Split(","c)
        ' Get the distinct values from the string array
        Dim uniqueNumbers = str.Distinct()

        ' Loop through the distince values and find what indexes they are located at
        For Each uniqueNumber In uniqueNumbers
            Dim indexValues As New List(Of Integer)
            For index = 0 To str.Length - 1
                If (uniqueNumber = str(index)) Then
                    indexValues.Add(index)
                End If
            Next

            ' Ouput the results of the distinct value
            Console.WriteLine("{0} is at indexes {1}", uniqueNumber, String.Join(", ", indexValues.ToArray()))
        Next
        Console.ReadLine()
    End Sub
End Module

结果:

1 is at indexes 0, 2, 5, 7
2 is at indexes 1, 3
3 is at indexes 4
0 is at indexes 6
4 is at indexes 8

更短的代码

Imports System

Module Module1
    Sub Main()
        Dim str() As String = "1,2,1,2,3,1,0,1,4".Split(","c)
        Dim uniqueNumbers = str.Distinct()

        For Each uniqueNumber In uniqueNumbers
            Dim indexValues = str.Select(Function(s, i) New With {i, s}).Where(Function(t) t.s = uniqueNumber).Select(Function(t) t.i)
            Console.WriteLine("{0} is at indexes {1}", uniqueNumber, String.Join(", ", indexValues.ToArray()))
        Next
        Console.ReadLine()
    End Sub
End Module

最短代码

Imports System

Module Module1
    Sub Main()
        Dim str() As String = "1,2,1,2,3,1,0,1,4".Split(","c)
        str.Distinct().ToList().ForEach(Sub(u) Console.WriteLine("{0} is at indexes {1}", u, String.Join(", ", str.Select(Function(s, i) New With {i, s}).Where(Function(t) t.s = u).Select(Function(t) t.i).ToArray())))
        Console.ReadLine()
    End Sub
End Module

最短代码说明:

1. The `String` "1,2,1,2,3,1,0,1,4" gets broken into a `String Array` using the comma as the delimiter.
 2. `str.Distinct()` returns a collection of unique values from the `String Array`. 
    2.1. Adding the `.ToList()` takes the collection of unique values and turns it into a `List` object.
    2.2. Adding the `.ForEach()` iterates through each item in the list.  Each item is stored in the variable "u" which is defined by `Sub(u)`.  We use `Sub` instead of `Function` because what is happening in the `ForEach` will not be returning a value.  Instead we display to the screen `Console.WriteLine()`.
    2.3.  Console.WriteLine takes a formatted String `"{0} is at indexes {1}"`.  The `{0}` and `{1}` are place holders for variables to fill in.
    2.4.  Place holder {0} is replaced by the "u" variable which will be whatever unique number the `.ForEach` is on.  Place holder {1} is replaced by the String.Join operation which is figuring out all the index locations of the variable "u".
    2.5.  `String.Join()` combines an array into a single `String` separated by a delimiter (In this case, ", " is the delimiter).
    2.6.  The array is created by `str.Select(Function(s, i) New With {i, s}).Where(Function(t) t.s = u).Select(Function(t) t.i).ToArray()`
        2.6.1.  The .Select(Function(s, i) New With {i, s}) is returning a collection of index values (i variable) for the unique letter (s variable).
        2.6.2.  The .Where(Function(t) t.s = u) filters the collection from 2.6.1 to index values and unique letter where the in variable s matches the unique letter (u) from the `.ForEach()` in 2.2.  
        2.6.3.  The last `.Select(Function(t) t.i)` return a collection of just the index values that were found for the unique letter.
        2.6.4.  The `.ToArray()` transforms the collection made from 2.6.1 - 2.6.3 into an `Array`, that the `String.Join()` transforms into a single string separated by ", ".