Boris Callens
Boris Callens

Reputation: 93397

LF without CR on windows

How can I create a string with a line feed character in it without adding a carriage return? Using the Environment.NewLine method on a windows machine returns a CR/LF combo. I need just the LF.

I know about the VBLF in Microsoft.VisualBasic library, but as a company policy we are not allowed to use it.

Upvotes: 2

Views: 631

Answers (3)

Ahmad
Ahmad

Reputation: 12737

You can add Carriage return/Line Feed separately

Dim myString As String = "This is my string"

myString &= vbCr   'This will only add CarrigeReturn character

Alternatively you can use the ASCII code for the Carriage Return which is 13 while Line Feed is 10

myString &= Chr(10)    'This will add LF
myString &= Chr(13)    'This will add CR

Upvotes: 0

Steven Doggart
Steven Doggart

Reputation: 43743

I would recommend using the constants in the ControlChars class to get the individual LF character. Also, I would recommend using the string builder:

Dim builder As New StringBuilder()
builder.Append("first line")
builder.Append(ControlChars.Lf)
builder.Append("second line")
builder.Append(ControlChars.Lf)
builder.Append("third line")

Upvotes: 2

hometoast
hometoast

Reputation: 11792

If you MUST be stuck with that limitation you can just pull the LF out of Environment.NewLine.

Environment.NewLine.Substring(1,1)

Upvotes: 0

Related Questions