tags:

views:

194

answers:

4

Now in my program in c# that simulates the working of a lan-messenger I need to show the people who are currently online with one remote host-name on each line. However the problem here is that in order to do that I am using the function

//edited this line out, can't find a .Append for listBox or .AppendLine
//listBox.Append(String );

listBox.Items.Add(new ListItem(String));

On doing so, the carriage return and new line character(ie.. \r and \n) are being displayed as tiny little rectangular boxes at the end of the hostname. How do I get rid of that?

+2  A: 

You could use:

listBox.Append(String.Replace("\r\n", ""));

to get rid of those characters

ghills
Have you had much luck utilizing the listBox.Append()?
RSolberg
A: 

You could use the Trim method of the string to remove unwanted characters from the start and end of the string. Either call it without parameters to remove all white-space (including like breaks), or pass a character array with the characters that you want removed.

Fredrik Mörk
A: 

Sounds like you're using some sort of TextBox in single line mode. If so, activate TextBox.Multiline.

VVS
+1  A: 

Another option would be to consider creating an extension method for the custom trim that you are trying to achieve.

/// <summary>
/// Summary description for Helper
/// </summary>
public static class Helper
{
    /// <summary>
    /// Extension Method For Returning Custom Trim
    /// No Carriage Return or New Lines Allowed
    /// </summary>
    /// <param name="str">Current String Object</param>
    /// <returns></returns>
    public static string CustomTrim(this String str)
    {
        return str.Replace("\r\n", "");
    }
}

Your line of code then looks like this:

listBox.Items.Add(new ListItem(myString.CustomTrim());
RSolberg
Adding such extensions will add lots of junk to medium size+ projects.Just because you can make extensions don't mean you should.
Carl Bergquist
Its hard to call re-use of code "junk"... Whatever floats your boat though...
RSolberg