2

I have a int array with for example {1,2,3,4} value.

I want to put this numbers into, for example, my list box like this :

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

Output :

After Inserting (3) 1234 page fault = 5

1234 is just an example. My array is much bigger.

How can I do that in c#?

1
  • Try to be expressive in plain english first then coding. Commented May 22, 2012 at 8:28

3 Answers 3

5

You can use String.Join (actually the IEnumerable<T> overload is taken):

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

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

You can use String.Format to build the text and to increase readability:

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);

Edit 2:

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

Yes, you can replace the output:

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

Edit 3:

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

Here we go ...

String.Join(", ", intArray.Select(i => i == -1 ? "empty" : i.ToString()));
Sign up to request clarification or add additional context in comments.

10 Comments

i'm new in c#, i dont know how place it among the string, can u edit my code in the first post and show me how? just like output of first post...thanks
Just FYI, you can use the String.Join overload that takes an object[] - saves one explicit step, same .NET version requirement
@skarmats: Thanks for the reminder. Edited my answer accordingly.
tbh, I don't know which overload is used. Could be the one that takes IEnumerable<T> ;) The general point remains. Maybe someone can enlighten me about the resolution chain
@skarmats: Good question, actually it's this third overload which takes IEnumerable<T>(my first approach took IEnumerable<String>).
|
2
string.Join(", ", intArray.Select(i => i.ToString())) 

3 Comments

i'm new in c#, i dont know how place it among the string, can u edit my code in the first post and show me how? just like output of first post...thanks
Like this: listBox2.Items.Add("After Inserting (" + page[i].ToString() + ') ' + string.Join(", ", intArray.Select(i => i.ToString())) + " Page Fault = " + pf.ToString());
can i replace some Character instead one number in array? my array : {1,2,3,4,-1"} output : 1,2,3,4,empty
2

string.Join works also with ToList()

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

output is = "12345"

EDIT: I don't know the name of your array, so I still use the above numbers example

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

EDIT:

To exclude numbers like -1 then

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

Note, added a comma to separate the numbers

2 Comments

i'm new in c#, i dont know how place it among the string, can u edit my code in the first post and show me how? just like output of first post...thanks
can i replace some Character instead one number in array? my array : {1,2,3,4,-1"} output : 1,2,3,4,empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.