54

I have a to do this:

AccountList.Split(vbCrLf)

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

thanks

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Luca Romagnoli
  • 12,145
  • 30
  • 95
  • 157

10 Answers10

99

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.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
David
  • 72,686
  • 18
  • 132
  • 173
  • 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 08 '10 at 14:11
  • 5
    the solution is: AccountList.Split(System.Environment.NewLine.ToCharArray()) – Luca Romagnoli Mar 08 '10 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 '16 at 19:49
  • @TomTom is totally right. 'System.Environment.NewLine' should not be encouraged as a decent alternative to vbCrLf. – Crono May 17 '17 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.

Neil N
  • 24,862
  • 16
  • 85
  • 145
10
AccountList.Split("\r\n");
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
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;
huha
  • 4,053
  • 2
  • 29
  • 47
  • Might be better to use ControlChars.CrLf, as that also includes ControlChars.Cr and ControlChars.Lf as chars (not strings). – jmoreno Jul 26 '22 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 '22 at 07:26
  • The result is identical, but it includes more options later. – jmoreno Jul 27 '22 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.

victorm1710
  • 1,263
  • 14
  • 11
4

Are you looking for

System.Environment.NewLine

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
4

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

Javier
  • 4,051
  • 2
  • 22
  • 20
2

try this:

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

or

AccountList.Split(new String[]{"\r\n"},System.StringSplitOptions.RemoveEmptyEntries);
Il Vic
  • 5,576
  • 4
  • 26
  • 37
Santosh Sharma
  • 122
  • 1
  • 9
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 );
    
Ghini Antonio
  • 2,992
  • 2
  • 25
  • 48
R.Alonso
  • 989
  • 1
  • 8
  • 9
-4
"FirstLine" + "<br/>" "SecondLine"