将字符串的1维数组转换为文本

时间:2015-02-06 22:15:29

标签: arrays vb.net

好的,所以目前我有这个代码,我尝试将ss放在引号位于其所说的地方#34;我希望SS添加到哪里"但它没有用,所以我调查了这个问题,没有任何帮助,我来找你们。我搞砸了我的最后一个问题,所以我希望我在这个问题上做得很好,但无论如何,这是我根据Eric Lippert's blog post使用的当前代码

Public Class Form1
Private Function CartesianProduct(Of T)(ParamArray sequences As T()()) As T()()

    ' base case: 
    Dim result As IEnumerable(Of T()) = {New T() {}}
    For Each sequence In sequences
        Dim s = sequence
        ' don't close over the loop variable 
        ' recursive case: use SelectMany to build the new product out of the old one 
        result = From seq In result
                 From item In s
                 Select seq.Concat({item}).ToArray()
    Next
    Return result.ToArray()
End Function
Dim s1 As String() = New String() {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
Dim s2 As String() = New String() {"1", "2", "3", "4", "5", "6", "7", "8", "9"}

Dim ss As String()() = CartesianProduct(s1, s1, s2, s2, s2, s2, s1, s1)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox1.Text = TextBox1.Text + "000" + "Where I want SS to be added to"
End Sub

结束班

2 个答案:

答案 0 :(得分:0)

您可以使用LINQ的Select方法将二维ss数组转换为单维字符串数组。例如,如果您只想连接第二维中的所有字符串而它们之间没有分隔符,则可以在String.Join中使用Select,如下所示:

Dim result As String() = ss.Select(Of String)(Function(x) "000" & String.Join("", x)).ToArray()

然后你可以使用它的1-D字符串数组并使用ListBox方法将其添加到AddRange,如下所示:

ListBox1.Items.AddRange(ss.Select(Of String)(Function(x) String.Join("", x)).ToArray())

答案 1 :(得分:0)

Public Class Form1
Dim MyMin As Integer = 1
Dim MyMax As Integer = 9
Dim My1stRandomNumber As Integer
Dim My2ndRandomNumber As Integer


Dim Generator As System.Random = New System.Random()
Dim prng As New Random
Const minCH As Integer = 2
Const maxCH As Integer = 2

Const randCH As String = "abcdefghijklmnopqrstuvwxyz"

Private Function RandomString() As String
    Dim sb As New System.Text.StringBuilder
    For i As Integer = 1 To prng.Next(minCH, maxCH + 1)
        sb.Append(randCH.Substring(prng.Next(0, randCH.Length), 1))
    Next
    Return sb.ToString()
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Timer1.Enabled = True

End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Label1.Text = Generator.Next(MyMin, MyMax + 1)
    Label2.Text = Generator.Next(MyMin, MyMax + 1)
    Label3.Text = Generator.Next(MyMin, MyMax + 1)
    Label4.Text = Generator.Next(MyMin, MyMax + 1)
    Label5.Text = RandomString()
    Label6.Text = RandomString()
    TextBox1.Text = TextBox1.Text + "$" + Label6.Text + Label1.Text + Label2.Text + Label3.Text + Label4.Text + Label5.Text
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Timer1.Enabled = False
End Sub

结束班

我是如何修复它只是使用多个标签切换整数类型触发和定时器循环。