这会更短吗?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了这会更短吗?相关的知识,希望对你有一定的参考价值。

这段代码太长了。如何缩短它?非常3中3,这将出现在最后。 TxtDrawRemize.Text & = TxtResultStr1.Text + "" + TxtResultStr2.Text + "" + TxtResultStr3.Text & Environment.NewLine

我想这么做,我陷入了代码中,因为它太长了,有时候我会误解变量。如果你能帮助我,我将不胜感激。

 Private Sub ScanareLinia1()
        On Error Resume Next
        Dim textsrtring As String = TxtStringNum1.Text
        Dim words As String() = textsrtring.Split(" "c)
        ' Split string based on space
        Dim found As Boolean = False
        ' Use For Each loop over words
        Dim word As Integer
        For Each word In words
            For i As Integer = 0 To TxtIntDraws.Lines.Count - 1
                If TxtIntDraws.Lines(1).Contains(word) Then
                    If Array.IndexOf(TxtIntDraws.Lines(1).Split(","c), CStr(word)) > -1 Then
                        TxtResultStr1.Text = word
                    End If
                End If
            Next
        Next
    End Sub
    Private Sub ScanareLinia2()
        On Error Resume Next
        Dim textsrtring As String = TxtStringNum2.Text
        Dim words As String() = textsrtring.Split(New Char() {" "c})
        ' Split string based on space
        Dim found As Boolean = False
        ' Use For Each loop over words
        Dim word As Integer
        For Each word In words
            For i As Integer = 0 To TxtIntDraws.Lines.Count - 1
                If TxtIntDraws.Lines(1).Contains(word) Then
                    If Array.IndexOf(TxtIntDraws.Lines(1).Split(","c), CStr(word)) > -1 Then
                        TxtResultStr2.Text = word
                    End If
                End If
            Next
        Next
    End Sub
    Private Sub ScanareLinia3()
        On Error Resume Next
        Dim textsrtring As String = TxtStringNum3.Text
        Dim words As String() = textsrtring.Split(New Char() {" "c})
        ' Split string based on space
        Dim found As Boolean = False
        ' Use For Each loop over words
        Dim word As Integer
        For Each word In words
            For i As Integer = 0 To TxtIntDraws.Lines.Count - 1
                If TxtIntDraws.Lines(1).Contains(word) Then
                    If Array.IndexOf(TxtIntDraws.Lines(1).Split(","c), CStr(word)) > -1 Then
                        TxtResultStr3.Text = word
                    End If
                End If
            Next
        Next
        TxtDrawRemize.Text &= TxtResultStr1.Text + " " + TxtResultStr2.Text + " " + TxtResultStr3.Text & Environment.NewLine
    End Sub
    Private Sub ScanareLinia4()
        On Error Resume Next
        Dim textsrtring As String = TxtStringNum1.Text
        Dim words As String() = textsrtring.Split(New Char() {" "c})
        ' Split string based on space
        Dim found As Boolean = False
        ' Use For Each loop over words
        Dim word As Integer
        For Each word In words
            For i As Integer = 0 To TxtIntDraws.Lines.Count - 1
                If Array.IndexOf(TxtIntDraws.Lines(2).Split(","c), CStr(word)) > -1 Then
                    TxtResultStr1.Text = word
                End If
            Next
        Next
    End Sub
    Private Sub ScanareLinia5()
        On Error Resume Next
        Dim textsrtring As String = TxtStringNum2.Text
        Dim words As String() = textsrtring.Split(New Char() {" "c})
        ' Split string based on space
        Dim found As Boolean = False
        ' Use For Each loop over words
        Dim word As Integer
        For Each word In words
            For i As Integer = 0 To TxtIntDraws.Lines.Count - 1
                If Array.IndexOf(TxtIntDraws.Lines(2).Split(","c), CStr(word)) > -1 Then
                    TxtResultStr2.Text = word
                End If
            Next
        Next
    End Sub
    Private Sub ScanareLinia6()
        On Error Resume Next
        Dim textsrtring As String = TxtStringNum3.Text
        Dim words As String() = textsrtring.Split(New Char() {" "c})
        ' Split string based on space
        Dim found As Boolean = False
        ' Use For Each loop over words
        Dim word As Integer
        For Each word In words
            For i As Integer = 0 To TxtIntDraws.Lines.Count - 1
                If Array.IndexOf(TxtIntDraws.Lines(2).Split(","c), CStr(word)) > -1 Then
                    TxtResultStr3.Text = word
                End If
            Next
        Next
        TxtDrawRemize.Text &= TxtResultStr1.Text + " " + TxtResultStr2.Text + " " + TxtResultStr3.Text & Environment.NewLine
    End Sub

以上是关于这会更短吗?的主要内容,如果未能解决你的问题,请参考以下文章

ReactJS 中的电子邮件验证无法正常工作 [重复]

编写VHDL优先级编码器的捷径

编写代码片段的更简洁的方法

按值传递列表以发挥作用

30 段 Python 实用代码

即学即用的 30 段 Python 实用代码