-3

I am initialising a StringBuilder as follows:

StringBuilder builder = new StringBuilder("Symptoms are ");

After this I loop through a list and add each string item to the end using

foreach(var item in list)
{
    builder.Append(item);
}

An example item would be something like "headache" but once i've appended it to "Symptoms are " and called builder.ToString() it shows:

"Symptoms areHeadache...etc"

as opposed to

"Symptoms are Headache...etc"

Why is it ignoring the space?

chead23
  • 1,859
  • 11
  • 12
  • 3
    I'm unable to replicate this issue. Would you post a small but complete program that produces the problem? – Dan J Nov 02 '12 at 16:35
  • 7
    `var builder = new StringBuilder("Symptoms are "); builder.Append("Headache"); var result = builder.ToString();` result is `"Symptoms are Headache"`. Can't reproduce. – dtb Nov 02 '12 at 16:35
  • If you manually add items with `.append()` outside of the `foreach` do you see the problem? – Grambot Nov 02 '12 at 16:41
  • 1
    My test code works fine: `var sb = new StringBuilder("abc "); var list = new List { "1", "2", "3" }; foreach (var item in list) { sb.Append(item); }` -- Produces "abc 123" – Jon B Nov 02 '12 at 16:42
  • Are you calling Trim anywhere? – Polyfun Nov 02 '12 at 16:45
  • 3
    I wouldn't be surprised if he's actually talking about a lack of whitespace between the _items_... in which case he ought to use something like `String.Format("Symptoms are {0}.", String.Join(", ", items))` – canon Nov 02 '12 at 16:46
  • 3
    I imagine all this is only making @UweKeim's headache worse. Hopefully the OP can clarify this mess. :P – Dan J Nov 02 '12 at 16:52

2 Answers2

1

Since the code you've provided doesn't produce the problem you've described, I'm guessing that your actual problem is a lack of whitespace between the symptoms (items). In that case, I'd suggest using String.Join() like so:

var output = String.Format("Symptoms are {0}.", String.Join(", ", items));

Now your items will be comma-separated with a padding space between.

canon
  • 40,609
  • 10
  • 73
  • 97
  • But your solution o/p : 'Symptoms are A, B, C' But Actual output should be 'Symptoms are A B C' – Prasad Kanaparthi Nov 02 '12 at 17:00
  • @PrasadKanaparthi He can provide whatever delimiter he'd like for `String.Join()`. He's not providing _any_ delimiter at the moment. (And it's actually `"Symptoms are A, B, C."` because I took some liberties with formatting.) – canon Nov 02 '12 at 17:00
  • You are suggesting String.Format, What is the wrong with AppendFormat ??? – Prasad Kanaparthi Nov 02 '12 at 17:03
  • @PrasadKanaparthi Because you've proposed calling `AppendFormat()` for each item in the collection... while I've called `String.Format` and `String.Join` only once. Also check out this answer http://stackoverflow.com/questions/585860/string-join-vs-stringbuilder-which-is-faster – canon Nov 02 '12 at 17:04
0

It does not need to eat the space on other PCs. Maybe try a new PC.

StringBuilder builder = new StringBuilder("Symptoms are ");
List<string> list = new List<string>() { "headache", "corrupt memory" };

foreach(var item in list)
{
        builder.Append(item);
}
System.Diagnostics.Debug.WriteLine(builder.ToString());
paparazzo
  • 44,497
  • 23
  • 105
  • 176