How to split a string into a fixed length string array?

22,794

Solution 1

This could work.

 Module Module1

    Sub Main()
        Dim LongString As String = "123abc456def789ghi"
        Dim longlist As New List(Of String)
        For i As Integer = 0 To Convert.ToInt32(LongString.Length / 3) - 1
            longlist.Add(LongString.Substring(i * 3, 3))
        Next
        For Each s As String In longlist
            Console.WriteLine(s)
        Next
        Console.ReadLine()
    End Sub

End Module

And this should work in .Net 1.1

Module Module1

    Sub Main()
        Dim LongString As String = "123abc456def789ghi"
        Dim longlist(Convert.ToInt32(LongString.Length / 3) - 1) As String
        For i As Integer = 0 To Convert.ToInt32(LongString.Length / 3) - 1
            longlist(i) = (LongString.Substring(i * 3, 3))
        Next
        For i As Integer = 0 To Convert.ToInt32(LongString.Length / 3) - 1
            Console.WriteLine(longlist(i))
        Next
        Console.ReadLine()
    End Sub

End Module

Solution 2

You could use LINQ like so:


' VB.NET
Dim str = "123abc456def789ghij"
Dim len = 3
Dim arr = Enumerable.Range(0, str.Length / len).Select (Function(x) str.Substring(x * len, len)).ToArray()


// C#
var str = "123abc456def789ghij";
var len = 3;
var arr = Enumerable.Range(0, str.Length / len).Select (x => str.Substring(x * len, len)).ToArray();

Note this will only take complete occurrences of length (i.e. 3 sets in a string 10 characters long).

Solution 3

This C# code should work:

public static string[] SplitByLength(string text, int length)
{
    // According to your comments these checks aren't necessary, but
    // I think they're good practice...
    if (text == null)
    {
        throw new ArgumentNullException("text");
    }
    if (length <= 0)
    {
        throw new ArgumentOutOfRangeException("length");
    }
    if (text.Length % length != 0)
    {
        throw new ArgumentException
            ("Text length is not a multiple of the split length");
    }
    string[] ret = new string[text.Length / length];
    for (int i = 0; i < ret.Length; i++)
    {
        ret[i] = text.Substring(i * length, length);
    }
    return ret;
}

Reflector converts that to VB as:

Public Shared Function SplitByLength(ByVal [text] As String, _
                                      ByVal length As Integer) As String()
    ' Argument validation elided
    Dim strArray As String() = New String(([text].Length \ length)  - 1) {}
    Dim i As Integer
    For i = 0 To ret.Length - 1
        strArray(i) = [text].Substring((i * length), length)
    Next i
    Return strArray
End Function

It's possible that that isn't idiomatic VB, which is why I've included the C# as well.

Solution 4

I'm splitting the string by 35.

var tempstore ="12345678901234567890123456789012345";

for (int k = 0; k < tempstore.Length; k += 35) {
    PMSIMTRequest.Append(tempstore.Substring(k,
      tempstore.Length - k > 35 ? 35 : tempstore.Length - k));
    PMSIMTRequest.Append(System.Environment.NewLine);
}

messagebox.Show(PMSIMTRequest.tostring());
Share:
22,794
Yoga Fire
Author by

Yoga Fire

Updated on July 08, 2020

Comments

  • Yoga Fire
    Yoga Fire almost 4 years

    I have a long string like this

    dim LongString as String = "123abc456def789ghi"
    

    And I want to split it into a string array. Each element of the array should be in 3 characters length

    For example,

    Dim LongArray(5) As String
    LongArray(0)  = "123"
    LongArray(1)  = "abc"
    LongArray(2)  = "456"
    LongArray(3)  = "def"
    LongArray(4)  = "789"
    LongArray(5)  = "ghi"
    

    How do I split it using VB.net code?