gpt4 book ai didi

c# - 在 C# 中的字符串中使用 int 数组

转载 作者:行者123 更新时间:2023-11-30 19:29:55 24 4
gpt4 key购买 nike

我有一个 int 数组,例如 {1,2,3,4} 值。

例如,我想将这些数字放入我的列表框中:

listBox2.Items.Add("After Inserting (" + page[i].ToString() + ')' + <use all my numbers like this : 1234 here>+"Page Fault = " + pf.ToString());  

输出:

After Inserting (3) 1234 page fault = 5

1234 只是一个例子。我的数组要大得多。

我如何在 C# 中做到这一点?

最佳答案

您可以使用 String.Join (实际上采用了 IEnumerable<T> 重载):

String joined = String.Join("", yourArray); 

i'm new in c# how i dont know how place the string among the text

您可以使用 String.Format 构建文本并提高可读性:

var inserted = page[i].ToString();
var allInserted = String.Join("", yourArray);
var pageFault = pf.ToString();
var itemText = String.Format("After Inserting ({0}) {1} page fault = {2}"
,inserted, allInserted, pageFault);
listBox2.Items.Add(itemText);

编辑 2:

can i replace some Character instead one number in array? my array : {1,2,3,4,-1"} output : 1,2,3,4,empty

是的,你可以替换输出:

String.Join("", yourArray.Where(i => i != -1));

编辑 3:

i understand how i can exclude -1 but i didn't understand how i can replace something with that...like "empty" instead -1

我们开始...

String.Join(", ", intArray.Select(i => i == -1 ? "empty" : i.ToString()));

关于c# - 在 C# 中的字符串中使用 int 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10698363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com