VB.net循环通过拆分字符串/替换

时间:2012-06-01 11:26:39

标签: vb.net arrays loops replace

我正在尝试在VB.net中创建一个循环算法的函数。我已经使用Split命令将算法拆分为数组,因此我有一个包含值的数组。 然后我尝试循环遍历它们并在必要时将#替换为“Number”,但是VB.net会抛出错误。 我是VB.net的新手,所以我不确定为什么要这样做。 算法的格式为A B C D E F 1 2 3 #

Function generate(ByVal alg As String)
    Dim algSplit As String() = alg.Split(" ")
    For Each digit In algSplit
        Dim replacement As String = algSplit(digit).Replace("#", "Number")
        algSplit(digit) = replacement
    Next
    Dim result As String = String.Join("", algSplit)
    MsgBox(result)
End Function

有快速修复吗?

问题第2部分:我让循环工作;但是我打破了我的目标部分功能。

Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
    Dim Generator As System.Random = New System.Random()
    Return Generator.Next(Min, Max)
End Function
Public Function RandLet() As String
    Dim number As Integer = GetRandom(1, 26)
    Dim Alphabet() 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 Letter As String = Alphabet(number)
    Return Letter
End Function
Function generate(ByVal alg As String) As String
    Dim algSplit As String() = alg.Split(" "c)

    For index As Int32 = 0 To algSplit.Length - 1
        algSplit(index) = algSplit(index).Replace("#"c, GetRandom(1, 9)).Replace("%"c, RandLet())
    Next
    Dim result As String = String.Join("", algSplit)
    MsgBox(result)
    Return result
End Function

A B C D E F 1 2 3 | L % % % | N # # #的最终结果最终为ABCDEF123|LXXX|N888 基本上,每个#和每个%每次都被相同的数字替换而不是不同的数字。我认为这是一个循环,这不会发生,我错过了什么?

4 个答案:

答案 0 :(得分:2)

你是For-Eaching一个字符串变量,因为digit找到“A”,这是一个字符串,但你将它用作索引:algSplit(digit).

你可以试试这个:

For i As Integer = 0 To algSplit.Length - 1
  algSplit(i) = algSplit(i).Replace("#", "Number")
Next

如果你只是想要替换#和空格,这也适用:

alg = alg.Replace(" ", String.Empty).Replace("#", "Number")

答案 1 :(得分:1)

使用LINQ可能更容易:

Dim value = "A B C D E F 1 2 3 #"
Dim replaced = From digit In value.Split(" "c)
             Select digit.Replace("#"c, "Number")
Dim result = String.Join("", replaced)

您的“传统”方法,已更正:

Function generate(ByVal alg As String) As String
    Dim algSplit As String() = alg.Split(" "c)

    For index As Int32 = 0 To algSplit.Length - 1
        algSplit(index) = algSplit(index).Replace("#"c, "Number")
    Next
    Dim result As String = String.Join("", algSplit)
    Return result
End Function
  1. 方法(VB中的函数)必须返回一些东西。您的函数不返回任何内容,因此最后添加AS String
  2. Split有多个重载,您使用的重载采用Char代替字符串,因此请将alg.Split(" ")替换为alg.Split(" "c)
  3. 如果您想用其他内容替换数组值,最好是For-Loop而不是For-Each,因为您需要经常使用索引(见上文)

答案 2 :(得分:1)

你正在循环一个字符串数组 algSplit上的foreach返回一个不是整数的字符串 您不能将digit字符串用作数组的索引

用以下代码替换您的代码:

Function generate(ByVal alg As String)     
    Dim algSplit As String() = alg.Split(" ")     
    For digit = 0 to algSplit.Length - 1
        Dim replacement As String = algSplit(digit).Replace("#", "Number")         
        algSplit(digit) = replacement     
               ' could be simply written as 
               ' algSplit(digit) = algSplit(digit).Replace("#", "Number")     

    Next     
    Dim result As String = String.Join("", algSplit)     
    ??? .. you should return something here ????
End Function 

答案 3 :(得分:-2)

Function generate(ByVal alg As String)     
    Dim algSplit As String() = alg.Split(" ")     
    For digit = 0 to algSplit.Length - 1
        Dim replacement As String = algSplit(digit).Replace("#", "Number")         
        algSplit(digit) = replacement     
               ' could be simply written as 
               ' algSplit(digit) = algSplit(digit).Replace("#", "Number")     

    Next     
    Dim result As String = String.Join("", algSplit)     
    ??? .. you should return something here ????
End Function