c#winforms如何重复字符串数组成员?(c# winforms how to repeat string array members?)

编程入门 行业动态 更新时间:2024-10-21 19:30:46
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.

更多推荐

本文发布于:2023-07-06 07:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047602.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   成员   winforms   members

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!