54

I have a to do this:

AccountList.Split(vbCrLf)

In c# AccountList is a string. How can i do?

thanks

10 Answers 10

101

You are looking for System.Environment.NewLine.

On Windows, this is equivalent to \r\n though it could be different under another .NET implementation, such as Mono on Linux, for example.

4
  • 11
    Theoretically bad answer.... this may change when run on Linux/Mono.... it is not "CRLF" but "the line separator defined on this computer type". No risk on Windows.... but then... ;)
    – TomTom
    Mar 8, 2010 at 14:11
  • 5
    the solution is: AccountList.Split(System.Environment.NewLine.ToCharArray()) Mar 8, 2010 at 16:56
  • I tried this: string[] strOptions = txtOptions.Text.Split(System.Environment.NewLine.ToCharArray()); and if there are four options by pressing enter after each, it returns 7 instance instead of 4.
    – Si8
    Mar 21, 2016 at 19:49
  • @TomTom is totally right. 'System.Environment.NewLine' should not be encouraged as a decent alternative to vbCrLf.
    – Crono
    May 17, 2017 at 14:46
13

I typically abbreviate so that I can use several places in my code. Near the top, do something like this:

 string nl = System.Environment.NewLine;

Then I can just use "nl" instead of the full qualification everywhere when constructing strings.

10
AccountList.Split("\r\n");
7

Add a reference to Microsoft.VisualBasic to your project.

Then insert the using statement

using Microsoft.VisualBasic;

Use the defined constant vbCrLf:

private const string myString = "abc" + Constants.vbCrLf;
3
  • Might be better to use ControlChars.CrLf, as that also includes ControlChars.Cr and ControlChars.Lf as chars (not strings).
    – jmoreno
    Jul 26, 2022 at 14:10
  • @jmoreno: The question was about vbCrLf. There is no need to use the ControlChars class. But the result is identical.
    – huha
    Jul 27, 2022 at 7:26
  • The result is identical, but it includes more options later.
    – jmoreno
    Jul 27, 2022 at 11:58
6

There is no equivalent of vbCrLf constant in C#. C#'s System.Environment.NewLine is an equivalent of vbNewLine in Visual Basic, it's NOT an equivalent of vbCrLf, because the value System.Environment.NewLine depends on OS where the C# application is running (and so does vbNewLine) - see msdn link as a reference.

In C# you can use "\r\n" instead of vbCrLf, as already was mentioned in one of the comments.

4

I think that "\r\n" should work fine

4

Are you looking for

System.Environment.NewLine

3

try this:

AccountList.Split(new String[]{"\r\n"},System.StringSplitOptions.None);

or

AccountList.Split(new String[]{"\r\n"},System.StringSplitOptions.RemoveEmptyEntries);
0
public  string VB2 = System.Environment.NewLine + System.Environment.NewLine;
public string VBCrLf = System.Environment.NewLine;

MessageBox.Show("Error: " + VB2 +  E.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation );
    
1
  • 1
    please add some explanation
    – mikus
    Sep 21, 2022 at 8:10
-4
"FirstLine" + "<br/>" "SecondLine"
2
  • 1
    This is not equivalent.
    – Wai Ha Lee
    Apr 20, 2016 at 17:30
  • HTML newline, only useful when C# is outputting to a webpage. Not useful in C# output to console or scripting, like in messagebox. Nov 8, 2020 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.