按字符数分割字符串并存储在字符串数组中 [英] Split string by character count and store in string array
本文介绍了按字符数分割字符串并存储在字符串数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的字符串
I have a string like this
abcdefghij
我当时并没有把这个字符串分成3个字符.我想要的输出将是一个包含此
And I wast to split this string by 3 characters each. My desired output will be a string array containing this
abc
def
ghi
j
是否可以使用 string.Split()
方法?
推荐答案
IEnumerable<string> GetNextChars ( string str, int iterateCount )
{
var words = new List<string>();
for ( int i = 0; i < str.Length; i += iterateCount )
if ( str.Length - i >= iterateCount ) words.Add(str.Substring(i, iterateCount));
else words.Add(str.Substring(i, str.Length - i));
return words;
}
这将避免在@Sajeetharan的答案中出现 ArgumentOutOfRangeException
.
This will avoid ArgumentOutOfRangeException
in @Sajeetharan's answer.
很抱歉,我以前的回答完全愚蠢:)这应该可以解决问题.
Sorry for completely dumb previous answer of mine :) this is supposed to do the trick.
这篇关于按字符数分割字符串并存储在字符串数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文