拆分的字符串不分离 [英] split a string without separator
本文介绍了拆分的字符串不分离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有值的字符串变量
abcdefghijklmnop。
I have a string variable with value
"abcdefghijklmnop".
现在我想和说的3个字符(最后一个数组元素可以包含以下)从右端每个数组元素的字符串分割成字符串数组。
now I want to split the string into string array with say 3 characters(last array element may contain less) in each array element from the right end.
也就是说,
一个
BCD
EFG
HIJ
荷航
NOP
ie.,
"a"
"bcd"
"efg"
"hij"
"klm"
"nop"
什么是最简单,最容易做到这一点的方法? (VB和C#codeS的欢迎)
what is easiest and simplest way to do this?? (both vb and C# codes are welcomed)
推荐答案
下面是一个解决办法:
var input = "abcdefghijklmnop";
var result = new List<string>();
int incompleteGroupLength = input.Length % 3;
if (incompleteGroupLength > 0)
result.Add(input.Substring(0, incompleteGroupLength));
for (int i = incompleteGroupLength; i < input.Length; i+=3)
{
result.Add(input.Substring(i,3));
}
给出的预期输出:
Gives expected output of:
"a"
"bcd"
"efg"
"hij"
"klm"
"nop"
这篇关于拆分的字符串不分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文