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

前端 未结 8 1829
别跟我提以往
别跟我提以往 2020-12-21 02:10

I have a long string like this

dim LongString as String = \"123abc456def789ghi\"

And I want to split it into a string array. Each element

相关标签:
8条回答
  • 2020-12-21 02:23

    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).

    0 讨论(0)
  • 2020-12-21 02:24

    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
    
    0 讨论(0)
  • 2020-12-21 02:25

    I have added some more logic to @jon code. This will work perfectly for the string which has length less than length passed.

     Public Shared Function SplitByLength(ByVal [text] As String, ByVal length As Integer) As String()
    
        Dim stringLength = text.Length
        Dim arrLength As Integer = ([text].Length \ length) - 1 + IIf(([text].Length 
                                                 Mod length) > 0, 1, 0)
        Dim strArray As String() = New String(arrLength) {}
    
        Dim returnString As String = ""
        Dim i As Integer
        Dim remLength As Integer = 0
    
        For i = 0 To strArray.Length - 1
          remLength = stringLength - i * length
          If remLength < length Then
            strArray(i) = [text].Substring((i * length), remLength)
          Else
            strArray(i) = [text].Substring((i * length), length)
          End If
        Next i
    
           Return  strArray
    END FUNCTION
    
    0 讨论(0)
  • 2020-12-21 02:26

    Last array is missing:

    Public 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 text.Length - 1
          strArray(i) = text.Substring((i * length), length)
      Next i
    
      ' Get last array:
      ReDim Preserve strArray(i)
      strArray(i) = text.Substring(i * length)
      Return strArray
    End Function
    
    0 讨论(0)
  • 2020-12-21 02:30

    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());
    
    0 讨论(0)
  • 2020-12-21 02:37

    This is my solution:

    Function splitN(str As String, n As Integer) As String()
      For i = 0 To Len(str) Step n + 1
        Try
          str = str.Insert(i + n, "|")
        Catch
        End Try
      Next
      Return str.Split("|")
    End Function
    

    You can call it like: splitN("abc123def456frg987ef", 3)

    0 讨论(0)
提交回复
热议问题