1

The moment you realize the .net Framework's String.TrimEnd() method will actually modify your String prior to returning it, and there seems to be no convenient way of getting a copy without declaring a new variable...

Just wanted to get rid of excess empty lines in the log caused by trailing <CR><LF> when receiving lines of serial data:

Console.WriteLine(DateTime.Now.ToString("H:m:s.fff") + " - serial data received on " + com_port + ": " + serial_data.TrimEnd())

But suddenly the parser could not find its termination characters anymore...
Resulting in probably the most disgusting parentheses I had ever added to any code:

Console.WriteLine(DateTime.Now.ToString("H:m:s.fff") + " - serial data received on " + com_port + (": " + serial_data).TrimEnd())

Yes, I feel bad about it, but then again is VB .net and it kinda "works for me". I promise I will (try to remember to) remove these as soon as debugging is done...

Comments
  • 2
    So you're the person who still uses vb.net!
  • 0
    Does vb.net have string interpolation, like c#?
    Like

    $"data: {serialdata.Trimend()}"

    Also aren't strings immutable? So. Trimend() can't change the string it is operating on?
  • 0
    @nibor I (still) have to, but my resignation letter is basically out, only a few more months of dealing with this clusterfuck :)

    String interpolation was apparently added in newer versions, but no chance when you're using 2010 Express Edition for production, since it's free...

    Also I think strings are immutable, but so what? TrimEnd would probably just create a new string in memory and throw away the reference to the previous one...
  • 0
    @ninjasloth the original string won't be freed up by the garbage collector as long as there is a reference to it. So if you do

    trimmedSerialData = serialData. Trimend()

    The serialData value will still be valid and present.
Add Comment