-1

I am trying to add a user input string to a StringBuilder I can then use elsewhere to display the string I'm building. The problem is when a user does a carriage return and I try to append that to the StringBuilder it removes the escape characters and therefore the return.

For example lets say a user enters the following:

Hello,

My name is Tom.

When I pull that string from the textbox control and pass it to my method that holds the StringBuilder it looks like this:

Hello,\r\nMy name is Tom. 

But once I append it to the StringBuilder it removes all escape characters and becomes this:

Hello,My name is Tom. 

This causes the output to be:

Hello,My name is Tom

instead of:

Hello,

My name is Tom

This is the method that pulls the user input from the ctrl

protected List<BetweenTagData> BetweenTagDataPull(string ctrlName)
{
    List<BetweenTagData> data = new List<BetweenTagData>();
    BetweenTagData pullIt = new BetweenTagData();

    if (ctrlName == "Label")
    {
        pullIt.text = TagLabelTxtbxEnterText.Text.ToString();
        data.Add(pullIt);
    }

    return data;
}

This is the method that builds the string using the StringBuilder

static public string TagBetween(List<BetweenTagData> betweenData)
{
    StringBuilder betweenString = new StringBuilder();
    foreach (BetweenTagData row in betweenData.ToList())
    {
        if (row.text != "")
        {
            betweenString.Append(row.text);
        }
    }

    return betweenString.ToString();
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Sentius
  • 7
  • 6
  • The problem is not with StringBuilder. At what point in your code have you determined the CRLF has been dropped? – Jasen Jan 22 '19 at 18:09
  • @Jasen If you follow the code line by line using a break point, it shows that row.fontSize has the correct string. as soon as it is appended to the stringbuilder the string changes and the escape characters are removed. – Sentius Jan 22 '19 at 18:18
  • I've run the debugger on `.Append()` and in my simple test it keeps "\r\n" after `.ToString()`. – Jasen Jan 22 '19 at 18:20
  • @Jasen I swear my head is filled with air sometimes. I posted the wrong methods. *sigh*. I've updated my post. Sorry. I'm currently debugging said code and that's why my brains being dyslectic. – Sentius Jan 22 '19 at 18:25
  • Why do you call `ToList()` when you _already have a list_, and `ToString()` when you _already have a string_? – Joel Coehoorn Jan 22 '19 at 20:51
  • @JoelCoehoorn The ToList() was an attempt to see if the compiler was just freaking out(my college professor called it F.M. F***ing Magic). As for the ToString() it is required as my method returns a string not a StringBuilder. If you write the code out in VS you'll see it red lines and says exactly that. – Sentius Jan 22 '19 at 21:46
  • Not that `ToString()`.This one: `TagLabelTxtbxEnterText.Text.ToString()` – Joel Coehoorn Jan 22 '19 at 23:47
  • @JoelCoehoorn was done for the same reason. Anyway I got it fixed. Commented below. – Sentius Jan 24 '19 at 18:10

1 Answers1

0

I'm not sure exactly what your problem is, but I can tell you with certainty the StringBuilder types does not care about or alter those character. The real problem is more likely in the BetweenTagData class. I can also help you improve the existing methods:

protected List<BetweenTagData> BetweenTagDataPull(string ctrlName)
{
    List<BetweenTagData> data = new List<BetweenTagData>();

    if (ctrlName == "Label")
    {
        data.Add(new BetweenTagData() {text = TagLabelTxtbxEnterText.Text});
    }

    return data;
}

static public string TagBetween(IEnumerable<BetweenTagData> betweenData)
{
    return string.Join("", betweenData.Where(bd => !string.IsNullOrEmpty(bd.text)).Select(bd => bd.text));
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • My BetweenTagData class literally just has public string text { get; set; } in it right now. I need to get this step working before I start to build on it. – Sentius Jan 22 '19 at 21:55
  • You where right that the problem was somewhere else in the code, but it wasn't the class. In the debugger it showed the string with no escape characters once appended to the StringBuilder,still don't know why, so I thought that was the problem. In fact it was a line of code out of place on the loop in another method that's manipulating the same string. Thank you for giving me the thought to look elsewhere. It just seemed like that was the issue, because once again, it showed the string with no escape characters in the debugger. – Sentius Jan 22 '19 at 23:29