c#winforms如何重复字符串数组成员?(c# winforms how to repeat string array members?)
首先,我需要从DataGridView.SelectedCells.Values创建一个字符串数组。 然后我需要将该字符串追加到自身,直到达到member.count的限制。 例如,如果
string [] = {"a", "b", "c"}; // Where abc are selectedCells.Values.新的字符串[]应该是:
{"a", "b", "c", "a", "b", "c", "a", "b"}
例如,如果限制为8。我怎么能解决这个问题呢?
First, I need to create a string array from a DataGridView.SelectedCells.Values. Then I need to append that string to itself, until a limit of member.count is reached. For example, if
string [] = {"a", "b", "c"}; // Where abc are selectedCells.Values.the new string [] should be:
{"a", "b", "c", "a", "b", "c", "a", "b"}
if limit is 8, for example.How could I solve this, please?
最满意答案
你可以在for循环中使用% (模数)。
string[] oldArr = new string[3] {"a","b","c"}; string[] newArr = new string[8]; int limit = 8; for ( int i = 0 ; i < limit ; i++ ) { newArr[i] = oldArr[i%oldArr.Length]; }而已。
you can use the % (Modulus) in a for loop.
string[] oldArr = new string[3] {"a","b","c"}; string[] newArr = new string[8]; int limit = 8; for ( int i = 0 ; i < limit ; i++ ) { newArr[i] = oldArr[i%oldArr.Length]; }That's it.
更多推荐
发布评论