【问题标题】:Converting 1-dimensional array of string to text将字符串的一维数组转换为文本
【发布时间】:2015-02-06 22:15:28
【问题描述】:

好的,所以目前我有这段代码,我试着把 ss 放在引号所在的地方,上面写着“我想在哪里添加 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

结束类

【问题讨论】:

  • 您希望它在输出中的格式如何?
  • 就像文本一样,如果你看看 ss 是如何制作的,我希望它按照上面使用的顺序出现,所以一个例子是 xx1234xx,我希望它添加到列表框中000 之前,所以我可以使用 Notepad++ 将其变为列表形式
  • 我注意到这些是单字符的字符串。如果将它们定义为字符而不是字符串,则实际上可以大大提高效率。
  • 供您参考,建议在 Visual Basic 中使用的字符串连接运算符为&,而不是+stackoverflow.com/questions/734600/…

标签: arrays vb.net


【解决方案1】:

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

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

然后您可以使用它的AddRange 方法将该一维字符串数组添加到ListBox,如下所示:

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

【讨论】:

  • 所以如果我这样做,那么它是否允许我使用算法“xx1234xx”1234 = 1-9 xx = a-z 使列表框/文本框有 8 个字符串
  • 对不起。你已经失去了我。我不明白你的意思。
  • 好吧,我的程序至少我想做的是使用 s1 和 s2 生成结果来制作 8 个字符组合我希望能够通过从我的程序中复制粘贴将它们保存到文本文档中文本框。
【解决方案2】:
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

结束类

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多