How to split a string by x amount of characters

后端 未结 4 488

I have a program where a user enters a list of numbers in the form of a string. This list of numbers is always a multiple of 8.

So the list can contain 8, 16, 32, 40

4条回答
  •  时光说笑
    2020-12-06 18:36

    You could use a For loop and Substring:

    Dim strings As New List(Of String)
    
    For i As Integer = 0 To Me.txtInput.Text.Length - 1 Step 8
        strings.Add(Me.txtInput.Text.Substring(i, 8))
    Next
    

    To convert the strings list to an array (if you really need one) you can use strings.ToArray().


    Also, you could use regular expressions and LINQ for a fancy one-liner:

    Text.RegularExpressions.Regex.Matches(Me.txtInput.Text, ".{8}").Select(Function(x) x.Value)
    

提交回复
热议问题