string 如何按x个字符数拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8774392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to split a string by x amount of characters
提问by Mark Kramer
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.
我有一个程序,用户可以在其中以字符串的形式输入数字列表。此数字列表始终是 8 的倍数。
So the list can contain 8, 16, 32, 40, 48, etc. numbers.
因此该列表可以包含 8、16、32、40、48 等数字。
I need to split that string into every 8 characters.
我需要将该字符串拆分为每 8 个字符。
For example, say the user entered "1234123445674567"
例如,假设用户输入“1234123445674567”
How can I split it into a string array where (0) is "12341234" and (1) is "45674567"
如何将其拆分为字符串数组,其中 (0) 为“12341234”,(1) 为“45674567”
Note:The size of the array has to be equal to the length of the string divided by 8.
注意:数组的大小必须等于字符串的长度除以 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.
编辑:我知道我可以通过创建一个循环来计算 8 个数字并将其拆分为一个数组来做到这一点,但这会很长并且需要一些变量,我知道有一种更有效的方法来做到这一点。我只是不知道语法。
采纳答案by dasblinkenlight
This should split the string into an array of 8-character substrings
这应该将字符串拆分为 8 个字符的子字符串数组
Dim orig = "12344321678900987"
Dim res = Enumerable.Range(0,orig.Length).[Select](Function(i) orig.Substring(i*8,8))
回答by Ry-
You could use a For
loop and Substring
:
您可以使用For
循环和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()
.
要将strings
列表转换为数组(如果您确实需要一个),您可以使用strings.ToArray()
.
Also, you could use regular expressions and LINQ for a fancy one-liner:
此外,您可以使用正则表达式和 LINQ 来制作精美的单行代码:
Text.RegularExpressions.Regex.Matches(Me.txtInput.Text, ".{8}").Select(Function(x) x.Value)
回答by Joshua Dannemann
Function slice(ByVal s as String) As String()
Return (From c As String in s).ToArray()
End Function
回答by Brad J
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