如何将字符串拆分为固定长度的字符串数组?

时间:2011-09-11 07:52:18

标签: .net vb.net string split .net-1.1

我有一个像这样的长字符串

dim LongString as String = "123abc456def789ghi"

我想把它拆分成一个字符串数组。数组的每个元素应该是3个字符长度

例如,

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

如何使用VB.net代码拆分它?

8 个答案:

答案 0 :(得分:4)

您可以像这样使用LINQ:


' 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();

请注意,这只会发生完整的长度(即字符串中的3组,长度为10个字符)。

答案 1 :(得分:2)

这可行。

 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

这应该适用于.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

答案 2 :(得分:2)

这个C#代码应该可以工作:

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将其转换为VB为:

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

这可能不是惯用的VB,这就是为什么我也包含了C#。

答案 3 :(得分:1)

我将字符串拆分为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());

答案 4 :(得分:0)

Dim LongString As String = "1234567"

Dim LongArray((LongString.Length + 2) \ 3 - 1) As String

For i As Integer = 0 To LongString.Length - 1 Step 3
    LongArray(i \ 3) = IF (i + 3 < LongString.Length, LongString.Substring(i, 3), LongString.Substring(i, LongString.Length - i))           
Next

For Each s As String In LongArray
    Console.WriteLine(s)
Next

有一些有趣的部分,使用\整数除法(总是向下舍入),事实上在VB.NET中你必须告诉DIM数组的最大元素(所以数组的长度为+1)(这对C#程序员来说很有趣)(并且它在昏暗中由-1解决),“+ 2”加法(我需要将除法舍入为3,所以我只是将2添加到被除数中,我可以使用三元运算符和模数,并在第一次测试中使用它),并使用三元运算符IF()来获取子串。

答案 5 :(得分:0)

缺少最后一个数组:

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

答案 6 :(得分:0)

我在@jon代码中添加了一些逻辑。这对于长度小于传递长度的字符串非常有效。

 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

答案 7 :(得分:0)

这是我的解决方案:

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

您可以这样称呼它:splitN("abc123def456frg987ef", 3)