10

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, 48, etc. numbers.

I need to split that string into every 8 characters.

For example, say the user entered "1234123445674567"

How can I split it into a string array where (0) is "12341234" and (1) is "45674567"

Note: The size of the array has to be equal to the length of the string divided by 8.

Like this:

Dim stringArray(txtInput.Text.Length/8) as String

Edit: I know I could do this by making a loop that counts 8 numbers and splits it into an array but that would be lengthy and take a few variables and I know there's a more efficient way to do it. I just don't know the syntax.

Mark Kramer
  • 3,134
  • 7
  • 34
  • 52

4 Answers4

8

This should split the string into an array of 8-character substrings

Dim orig = "12344321678900987"
Dim res = Enumerable.Range(0,orig.Length\8).[Select](Function(i) orig.Substring(i*8,8))
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 6
    By the way, VB is smart enough not to need `[Select]`. – Ry- Jan 08 '12 at 15:46
  • @minitech I have no idea of the VB syntax - I translated this query automatically from C# :) – Sergey Kalinichenko Jan 08 '12 at 17:08
  • 4
    Yeah, nobody wants to write VB anymore :) Keywords in VB following a `.` don't need to be escaped with `[]` is all. So you could make it a little neater with `.Select` instead. – Ry- Jan 08 '12 at 17:10
  • Note: Your string length must be divisible by the substring length for this solution to work. Otherwise, it will drop the last substring. – Burhan Jul 13 '22 at 23:05
6

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)
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • I like your answer more than the other guys. Especially the one-liner seems very efficient. Are there any extra things I have to add or import into my program to make it work? I'm going to test it and if it works I'll accept your answer – Mark Kramer Jan 08 '12 at 07:52
  • 1
    @MarkKramer: No, there shouldn't be. The one-liner needs LINQ, which is only available in .NET 3.5 and up (VB 2008 or higher) but the first one works in practically everything. – Ry- Jan 08 '12 at 15:47
  • Is LINQ something the user needs installed on their computer? Or just something I need on mine that comes with Visual Studio to compile the code – Mark Kramer Jan 08 '12 at 19:57
  • 1
    @MarkKramer: It comes with .NET 3.5, so if you can use it in your application at compile-time you can use it on anybody else's computer that your code would normally run on, yes. – Ry- Jan 08 '12 at 20:36
  • Okay. Also, I've got another question for you. I made my program using Visual Studio 2010 pro. Does my program require the .Net Framework 4.0 to work? – Mark Kramer Jan 08 '12 at 20:47
  • @MarkKramer: Most likely. You can change it, though, in My Project > Compile > Advanced Settings. – Ry- Jan 08 '12 at 21:37
1

To expand on the accepted answer, this will split a string into parts even if the string isn't divisible by the divisor

    Public Function SplitInParts(s As String, partLength As Integer) As IEnumerable(Of String)
        If String.IsNullOrEmpty(s) Then
            Throw New ArgumentNullException("String cannot be null or empty.")
        End If
        If partLength <= 0 Then
            Throw New ArgumentException("Split length has to be positive.")
        End If
        Return Enumerable.Range(0, Math.Ceiling(s.Length / partLength)).Select(Function(i) s.Substring(i * partLength, If(s.Length - (i * partLength) >= partLength, partLength, Math.Abs(s.Length - (i * partLength)))))
    End Function
Brad J
  • 31
  • 5
0
Function slice(ByVal s as String) As String()
    Return (From c As String in s).ToArray()
End Function
nhahtdh
  • 55,989
  • 15
  • 126
  • 162