如何按x个字符数拆分字符串

如何按x个字符数拆分字符串

本文介绍了如何按x个字符数拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,用户可以在其中输入字符串形式的数字列表.此数字列表始终是 8 的倍数.

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、16、32、40、48 等数字.

So the list can contain 8, 16, 32, 40, 48, etc. numbers.

我需要将该字符串拆分为每 8 个字符.

I need to split that string into every 8 characters.

例如,假设用户输入了1234123445674567"

For example, say the user entered "1234123445674567"

如何将其拆分为字符串数组,其中 (0) 为12341234",(1) 为45674567"

How can I split it into a string array where (0) is "12341234" and (1) is "45674567"

注意:数组的大小必须等于字符串的长度除以 8.

Note: The size of the array has to be equal to the length of the string divided by 8.

像这样:

Dim stringArray(txtInput.Text.Length/8) as String

我知道我可以通过创建一个循环来计算 8 个数字并将其拆分为一个数组来完成此操作,但这会很长并且需要一些变量,而且我知道有一种更有效的方法可以做到这一点.我只是不知道语法.

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 个字符的子字符串数组

This should split the string into an array of 8-character substrings

Dim orig = "12344321678900987"
Dim res = Enumerable.Range(0,orig.Length\8).[Select](Function(i) orig.Substring(i*8,8))

这篇关于如何按x个字符数拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:29