1

Basically I have this which I know won't work but I it illustrates what I'm trying to do:

MessageBox.Show("Found these: " + keywords[i] + " keywords.");

And I need to see this:

Found these: Item1, Item2 keywords.

There may be 1 keyword there may be 4, how should I do this?

Many thanks.

2 Answers 2

10

You could use string.Join:

MessageBox.Show("Found these: " + string.Join(", ", keywords) 
                + " keywords.");
Sign up to request clarification or add additional context in comments.

Comments

0

Jon Skeet has a good answer with string.Join. your other option for more complicated formatting would be to use a string builder

StringBuilder sb = new StringBuilder();
seperator = "";
foreach(string current in keywords){
 sb.Append(seperator);
 sb.Append(current);
 seperator = ", ";
}

MessageBox.Show("Found these: " + sb.ToString() + " keywords.");

1 Comment

sb.Length -= 2; // Remove the last ", " Should only happen when a string is in keywords else sb has a negative length and i dont know how it handles it :P

Your Answer

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