我怎样才能获得String值而不是整数

时间:2014-07-07 01:52:41

标签: vb.net

如何获取StartString和EndString

 Dim startNumber As Integer
    Dim endNumber As Integer
    Dim i As Integer

    startNumber = 1
    endNumber = 4

    For i = startNumber To endNumber

        MsgBox(i)

    Next i

输出:1,2,3,4 我想让mo像样本一样:startString AAA endString AAD 输出为AAA,AAB,AAC,AAD

6 个答案:

答案 0 :(得分:1)

这是一个易于理解和使用的简单功能。每次调用它时,它只会将字符串增加一个值。只需要小心检查文本框中的值,否则你手上就会有无限循环。

Function AddOneChar(Str As String) As String
    AddOneChar = ""
    Str = StrReverse(Str)
    Dim CharSet As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    Dim Done As Boolean = False
    For Each Ltr In Str
        If Not Done Then
            If InStr(CharSet, Ltr) = CharSet.Length Then
                Ltr = CharSet(0)
            Else
                Ltr = CharSet(InStr(CharSet, Ltr))
                Done = True
            End If
        End If
        AddOneChar = Ltr & AddOneChar
    Next
    If Not Done Then
        AddOneChar = CharSet(0) & AddOneChar
    End If
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim S = TextBox1.Text
    Do Until S = TextBox2.Text
        S = AddOneChar(S)
        MsgBox(S)
    Loop
End Sub

答案 1 :(得分:1)

这可以作为给定任意字母的所有代码的一种方式:

Public Function Generate(starting As String, ending As String, alphabet As String) As IEnumerable(Of String)
    Dim increment As Func(Of String, String) = _
        Function(x)
            Dim f As Func(Of IEnumerable(Of Char), IEnumerable(Of Char)) = Nothing
            f = _
                Function(cs)
                    If cs.Any() Then
                        Dim first = cs.First()
                        Dim rest = cs.Skip(1)
                        If first = alphabet.Last() Then
                            rest = f(rest)
                            first = alphabet(0)
                        Else
                            first = alphabet(alphabet.IndexOf(first) + 1)
                        End If
                        Return Enumerable.Repeat(first, 1).Concat(rest)
                    Else
                        Return Enumerable.Empty(Of Char)()
                    End If
                End Function
            Return New String(f(x.ToCharArray().Reverse()).Reverse().ToArray())
        End Function
    Dim results = New List(Of String)
    Dim text = starting
    While True
        results.Add(text)
        If text = ending Then
            Exit While
        End If
        text = increment(text)
    End While
    Return results
End Function

我这样使用它来产生所需的结果:

Dim alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

Dim results = Generate("S30AB", "S30B1", alphabet)

这给了我63个值:

S30AB 
S30AC 
...
S30BY 
S30BZ 
S30B0 
S30B1 

现在应该很容易根据需要修改字母表并使用结果。

答案 2 :(得分:0)

一种选择是将这些String值放入数组中,然后使用i作为该数组的索引,以便每次迭代获得一个元素。如果你这样做,请记住数组索引从0开始。

您还可以使用For Each循环访问数组的每个元素,而无需索引。

答案 3 :(得分:0)

如果输出的默认前两个字符串值为AA

您可以拥有caseif-else条件声明: 然后设置1 == A 2 == B ...

只需添加或连接你的默认两个字符串和你的案例的结果字符串。

答案 4 :(得分:0)

在您的情况下,为了实现目标,您可以做这样的事情

  Dim S() As String = {"AAA", "AAB", "AAC", "AAD"}

    For Each el In S
        MsgBox(el.ToString)
    Next

修复以前的问题

 Dim s1 As String = "AAA"

    Dim s2 As String = "AAZ"

    Dim Last As String = s1.Last
    Dim LastS2 As String = s2.Last
    Dim StartBase As String = s1.Substring(0, 2)
    Dim result As String = String.Empty
    For I As Integer = Asc(s1.Last) To Asc(s2.Last)

        Dim zz As String = StartBase & Chr(I)
        result += zz & vbCrLf
        zz = Nothing
        MsgBox(result)
    Next

   **UPDATE CODE VERSION**

  Dim BARCODEBASE As String = "SBA0021"
    Dim BarCode1 As String = "SBA0021AA1"
    Dim BarCode2 As String = "SBA0021CD9"
    'return AA1
    Dim FirstBarCodeSuffix As String = Replace(BarCode1, BARCODEBASE, "")
    'return CD9
    Dim SecondBarCodeSuffix As String = Replace(BarCode2, BARCODEBASE, "")
    Dim InternalSecondBarCodeSuffix = SecondBarCodeSuffix.Substring(1, 1)

    Dim IsTaskCompleted As Boolean = False

    For First As Integer = Asc(FirstBarCodeSuffix.First) To Asc(SecondBarCodeSuffix)
        If IsTaskCompleted = True Then Exit For
        For Second As Integer = Asc(FirstBarCodeSuffix.First) To Asc(InternalSecondBarCodeSuffix)
            For Third As Integer = 1 To 9
                Dim tmp = Chr(First) & Chr(Second) & Third
                Console.WriteLine(BARCODEBASE & tmp)
                If tmp = SecondBarCodeSuffix Then
                    IsTaskCompleted = True
                End If

            Next
        Next
    Next
    Console.WriteLine("Completed")
    Console.Read()

看看这个检查它,让我知道它是否可以提供帮助

答案 5 :(得分:0)

我试图了解您正在寻找使用2个文本框之间的范围的系列。以下是将采用该系列的代码,并根据需要提供输出。

Dim startingStr As String = Mid(TextBox1.Text, TextBox1.Text.Length, 1)
Dim endStr As String = Mid(TextBox2.Text, TextBox2.Text.Length, 1)

Dim outputstr As String = String.Empty

Dim startNumber As Integer
Dim endNumber As Integer

startNumber = Asc(startingStr)
endNumber = Asc(endStr)

Dim TempStr As String = Mid(TextBox1.Text, 1, TextBox1.Text.Length - 1)

Dim i As Integer
For i = startNumber To endNumber
    outputstr = outputstr + ", " + TempStr + Chr(i)
Next i

MsgBox(outputstr)

前两行将取消文本框中字符串的最后一个字符。

所以在你的情况下,它将分别获得A和D

然后outputstr创建我们将在循环中使用的系列

StartNumberEndNumber将为我们提取的字符提供Ascii值。

TempStr存储系列字符串之外的字符串,就像我们的情况一样AAA - AAD Tempstr将有AA

然后简单的循环来修复所有项目并显示