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

前端之家收集整理的这篇文章主要介绍了.net – 如何将字符串拆分为固定长度的字符串数组?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个很长的字符串
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代码拆分它?

这可行.
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
原文链接:/vb/255568.html

猜你在找的VB相关文章