English 中文(简体)
C# 字符串中使用 int数组
原标题:use int array in string in c#
  • 时间:2012-05-22 08:25:10
  •  标签:
  • c#

我有一个内数组, 例如 {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只是一个例子 我的阵列要大得多

我怎样才能做到呢?

最佳回答

您可以使用 < a href=" "http://msdn.microsoft.com/ en- us/library/dd992421.aspx" rel = “ no follown noreferr"\\ code>String. Join (实际上采用了 IEnumberable<T> 超载) :

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

为何我不知道如何将字符串放在文字中?

您可以使用 < a href=" "http://msdn.microsoft.com/ en- us/library/b1csw23d.aspx" rel = “ 不跟随 noreferr"\\ code>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);

<% 1 > 编辑 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));

<% 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()));
问题回答
string.Join(", ", intArray.Select(i => i.ToString())) 

字符串Join 和 ToList () 一起工作

int[] numbers = new int[] {1,2,3,4,5};
string s = string.Join("", numbers.ToList());
Console.WriteLine(s);

输出为 = "12345"

我不知道您数组的名称, 所以我仍然使用上述数字示例

listBox2.Items.Add("After Inserting (" + page[i].ToString() + ") " + 
                    string.Join("", numbers.ToList()) + 
                    " Page Fault = " + pf.ToString());   

编辑:

排除像 -1 这样的数字

int[] numeri = new int[] {1,2,3,4,5,-1};
string s = string.Join(",", numeri.Where(i => i != -1).ToList());
Console.WriteLine(s);

注,添加逗号以分开数字





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签