Formatting a string

hello all,

Can u please tell me quivalent of below string in C#.NET

strVehiNumber & " " & strVehiLocation & Chr(13) & strVehiTime & Chr(13) &strVehiSpeed & " Kmph"

specifically,How to insert chr(13) in my C#.NET string

Thanks ,

Anil Dhiman




Answer this question

Formatting a string

  • savage1965

    In C# you can insert any hexadecimal value in a string using the \xHH[HH] escape sequence like so:

    string str = "Hello\x0DWorld.";

    However if you are trying to put a newline in then you should use the \n, \r or \l escape characters instead. \l I believe maps to 0x0D. Normally you use \n which maps properly irrelevant of the output device otherwise you might need to handle the case where the console expects \r\l while files can normally handle just \r. Therefore the equivalent C# would be along the lines of:

    strNum + " " + strLoc + "\n" + strTime + "\n" + strSpeed + " Kmph"

    There is a running discussion on the efficient of this line of code using concatenation vs. using String.Format(). String.Concat() could also be used but has no real benefit over the + operator in this case. Personally because you are intermixing values with strings and it is rather long I'd use a more concise (albeit slower) line like:

    String.Format("{0} {1}\n{2}\n{3} Kmph", strNum, strLoc, strTime, strSpeed);

    The nice thing about this is that the variables can actually be any type and not necessarily a string.

    Michael Taylor - 5/11/06


  • Boonster

    Well i use this method:

    chr(13) is \r (or "\r" as it sits in a string)

    and chr(10) is \n

     



  • Formatting a string