0

This may seem simple.

It could be vbNewLine

or it can be

https://learn.microsoft.com/en-us/dotnet/api/system.environment.newline?redirectedfrom=MSDN&view=net-6.0#System_Environment_NewLine

However, that is NOT equivalent with "\n"

That is equivalent with

\r\n for non-Unix platforms, or \n for Unix platforms.

What about if I want \n no matter what. \

I tried to search for similar questions and I can't even find it.

There is nothing here either.

https://learn.microsoft.com/en-us/dotnet/api/system.environment.newline?redirectedfrom=MSDN&view=net-6.0#System_Environment_NewLine

So not easy to fine.

Update: One answer says that "\n" means vbNewLine both in windows and in Linux.

Well, I am writing a vb.net windows program that interact with linux machine. You know, usual API stuff. In which case I need a character in windows that always mean "\n" in linux.

Basically, I need the chr(10) character. Not chr(10)+chr(13) character.

I think the answer I wrote my self is the answer to that.

And I do not think there is a simple answer on that.

Differences Between vbLf, vbCrLf & vbCr Constants may make things clear. However, people that find that question are people that already guess that vbLf may be a solution.

In fact, the questions and the answers over there do not even link "\n" to vbLF at all. They just say that vbLF is line feed. Is it "\n"? Another technicality

This question answer the question more directly. So what's equivalent to linux/unix "\n" no matter what is vbLf

user4951
  • 32,206
  • 53
  • 172
  • 282
  • Did you try [vbCrLf](https://learn.microsoft.com/pl-pl/dotnet/api/microsoft.visualbasic.constants.vbcrlf?view=net-6.0) – Michał Turczyn Oct 26 '22 at 15:44
  • 1
    [Constants.vbLf](https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.constants.vblf) - Or use `ChrW(10)` directly – Jimi Oct 26 '22 at 15:53
  • The following may be helpful: https://stackoverflow.com/a/71364280/10024425 – Tu deschizi eu inchid Oct 26 '22 at 16:03
  • Ah.... Yap. vbLf. I forget. – user4951 Oct 26 '22 at 16:18
  • Does this answer your question? [Differences Between vbLf, vbCrLf & vbCr Constants](https://stackoverflow.com/questions/27223228/differences-between-vblf-vbcrlf-vbcr-constants) – Lennart Oct 26 '22 at 17:06
  • It kind of does but not explicitly. It's a different question. Answer to this more directly answer that question. For example, someone that use that question already know what vbLF is. Someone like me that look, hmmm... there is \n in c#, what should I replace that with in vb.net will not look for vbLf. Someone look for vbLf already knows that vbLf might be the answer – user4951 Oct 26 '22 at 17:10

3 Answers3

2
using Microsoft.VisualBasic.CompilerServices;

namespace Microsoft.VisualBasic
{
        // Summary:
        //     Represents a linefeed character for print and display functions.
        [__DynamicallyInvokable]
        public const string vbLf = "\n";
}

So the answer is

Microsoft.VisualBasic.vbLf

Somehow I can just use vbLf because Microsoft.VisualBasic is so often used it's in my project list I guess.

Update: One answer says that "\n" means vbNewLine both in windows and in Linux.

Well, I am writing a vb.net windows program that interact with linux machine. You know, usual API stuff. In which case I need a character in windows that always mean "\n" in linux.

Basically, I need the chr(10) character. Not chr(10)+chr(13) character.

I think vbLf is the right answer.

And I do not think there is a simple answer on that.

Differences Between vbLf, vbCrLf & vbCr Constants may make things clear. However, people that find that question are people that already guess that vbLf may be a solution.

This question answer the question more directly. So what's equivalent to linux/unix "\n", which is the line feed chr(10) character no matter what is vbLf

user4951
  • 32,206
  • 53
  • 172
  • 282
1

For VB.Net, which tends to run on Windows, the closest map to \n is vbCrLf. This is different than just vbLf, because vbLf always maps directly to the ascii line feed character (10), but \n on some platforms will map to whatever the local system uses for line endings, rather than a simple line feed. On Windows, this is typically the 13/10 vbCrLf pair.

The easiest way to include these in code strings is via the new-ish interpolated strings:

$"This string{vbCrLf}includes some{vbCrLf}line breaks."

If you want to go platform-agnostic, the closest match is Environment.NewLine. And since that's a mouthful to use over and over you can always assing the value to a variable with a shorter name, like this:

Dim vbNl As String = Environment.NewLine
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Based on the question: *What about if I want `\n` no matter what*, this mapping, as described here, is not clear, IMO. It appears that in Windows `\n` is transformed into `\r\n`, when you clearly mean that - in Windows - if you want to have the same effect of `\n` in *nix, you need to use `\r\n` (which applies in most cases, but not all) -- I didn't downvote, btw, but - still based on the question - the answer may need some adjustments – Jimi Oct 26 '22 at 17:10
  • Oh really? I didn't know that "\n" mean vbNewline in windows. – user4951 Oct 26 '22 at 17:11
  • It's technically correct. I updated the question due to this. Basically I want something that's equivalent with "\n" in Unix. Btw, is this even true? Is "\n" in c#, for example, mean vbCRLf? Can anyone confirm – user4951 Oct 27 '22 at 11:54
1

You ought to be using the ControlChars class in VB.NET. ControlChars.Lf is a line feed, i.e. equivalent to "\n", while ControlChars.Cr is a carriage return, i.e. equivalent to "\r". ControlChars.CrLf and ControlChars.NewLine are both equivalent to "\r\n". Environment.NewLine will give you "\r\n", "\n" or "\r", depending on the platform.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46