0

how do I make wpf label print out arrays of strings by using only one label text. I try to loop the answer by putting foreach. however it only print the last string of the array.

this is the example:

input : h t t p

string word = input_box.Text;
string[] split = word.Split(new char[] { ',', ' ' });
foreach (string s in split)
{
     if (s.Trim() != " ")
        label1.Content = s;
}

output:

p

can anyone please help me?

1 Answer 1

4

use label1.Content += s; instead of label1.Content = s;

but better would be using a StringBuilder

string word = input_box.Text;
string[] split = word.Split(new char[] { ',', ' ' });
StringBuilder sb = new StringBuilder();
foreach (string s in split)
{
     if (s.Trim() != " ")
         sb.Append(s);
}

label1.Content = sb.ToString();
Sign up to request clarification or add additional context in comments.

2 Comments

String.Join(String.Empty, word.Split(new char[] { ',', ' ' }).Where(s => s.Trim() != String.Empty)) ?
hi jens, thanks for the tip and also the additional tip to shortened the code. Actually I wanted to print out the output into arrays like this h t t p

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.