Arrays 在vb.net中获取特定字符串的索引以及字符串?
从上面的字符串数组中,我想知道重复字符串的索引,如“1”位于索引0,2处,“2”位于1,3处。如何在vb.net中执行此操作Arrays 在vb.net中获取特定字符串的索引以及字符串?,arrays,vb.net,Arrays,Vb.net,从上面的字符串数组中,我想知道重复字符串的索引,如“1”位于索引0,2处,“2”位于1,3处。如何在vb.net中执行此操作 请记住,我将从数据库中获取字符串数组。因此,我不知道哪些数字出现超过2次。听起来您需要在从数据库获取字符串后对其进行拆分,以便生成一个数组 然后,您需要知道数组中的所有不同的值,以便可以搜索并找到索引 Dim str() as string = "1,2,1,2" 结果: Imports System Module Module1 Sub Main()
请记住,我将从数据库中获取字符串数组。因此,我不知道哪些数字出现超过2次。听起来您需要在从数据库获取字符串后对其进行
拆分,以便生成一个数组
然后,您需要知道数组中的所有不同的值,以便可以搜索并找到索引
Dim str() as string = "1,2,1,2"
结果:
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
短码
最短代码
最短代码说明:
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
听起来您需要在从数据库获取字符串后对其进行拆分
,这样您就有了一个数组
然后,您需要知道数组中的所有不同的值,以便可以搜索并找到索引
Dim str() as string = "1,2,1,2"
结果:
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
短码
最短代码
最短代码说明:
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
听起来您需要在从数据库获取字符串后对其进行拆分
,这样您就有了一个数组
然后,您需要知道数组中的所有不同的值,以便可以搜索并找到索引
Dim str() as string = "1,2,1,2"
结果:
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
短码
最短代码
最短代码说明:
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
听起来您需要在从数据库获取字符串后对其进行拆分
,这样您就有了一个数组
然后,您需要知道数组中的所有不同的值,以便可以搜索并找到索引
Dim str() as string = "1,2,1,2"
结果:
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
短码
最短代码
最短代码说明:
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
这不是数组的有效声明。你的意思是Dim str As String()={“1”、“2”、“1”、“2”}
?bro实际上它不是一个数组。它是一个字符串值,每次我都会添加“,”取决于数据库记录。这不是数组的有效声明。你的意思是Dim str As String()={“1”、“2”、“1”、“2”}
?bro实际上它不是一个数组。它是一个字符串值,每次我都会添加“,”取决于数据库记录。这不是数组的有效声明。你的意思是Dim str As String()={“1”、“2”、“1”、“2”}
?bro实际上它不是一个数组。它是一个字符串值,每次我都会添加“,”取决于数据库记录。这不是数组的有效声明。你的意思是Dim str As String()={“1”、“2”、“1”、“2”}
?bro实际上它不是一个数组。这是一个字符串值,每次我都会添加“,”取决于数据库记录。非常感谢兄弟。但是有没有可能减少代码长度???兄弟,我是vb.net的新手。你能说一下如何在“模块”中运行代码吗?@nasr18这是一个示例控制台应用程序我明白了,兄弟。非常感谢你@Shar1er80@nasr18不客气。。。如果你发现我的答案有用,请点击我的答案,并考虑投票。非常感谢你,兄弟。但是有没有可能减少代码长度???兄弟,我是vb.net的新手。你能说一下如何在“模块”中运行代码吗?@nasr18这是一个示例控制台应用程序我明白了,兄弟。非常感谢你@Shar1er80@nasr18不客气。。。如果你发现我的答案有用,请点击我的答案,并考虑投票。非常感谢你,兄弟。但是有没有可能减少代码长度???兄弟,我是vb.net的新手。你能说一下如何在“模块”中运行代码吗?@nasr18这是一个示例控制台应用程序我明白了,兄弟。非常感谢你@Shar1er80@nasr18不客气。。。如果你发现我的答案有用,请点击我的答案,并考虑投票。非常感谢你,兄弟。但是有没有可能减少代码长度???兄弟,我是vb.net的新手。你能说一下如何在“模块”中运行代码吗?@nasr18这是一个示例控制台应用程序我明白了,兄弟。非常感谢你@Shar1er80@nasr18不客气。。。如果你发现我的答案有用,请点击我的答案,并考虑投票。