简体   繁体   中英

How to convert VB.net Constants to C#?

I am converting VB.Net project to C# in VS2013. I come across a problem in converting constants in VB.NET to C# code and don't want to use the refernce 使用引用 in my code. Please suggest me some links for this. check the examples below

For Constants.vbCrLf i use Environment.Newline;
Constants.vbCr = ?
Constants.vbLf = ?

string[] rows = AllData.Split(Constants.vbCr + Constants.vbLf.ToCharArray());

There's no need for such constants in C#, since they are character literals: \\r and \\n , respectively.

Environment.Newline isn't really CRLF ( \\r\\n in C#) - it's environment dependent. Sometimes it's what you want, sometimes it isn't.

EDIT:

To address your newly posted sample code, you could use this:

var rows = AllData.Split(new [] { "\r\n" }, StringSplitOptions.None);

Only you know whether "\\r\\n" or Environment.NewLine is the better option - if your input data is environment dependent, use Environment.NewLine . If it's supposed to always be "\\r\\n" , use that.

You could do it like this:

string AllData = @"I'm
a 
multi-line
string";

string[] rows = AllData.Split('\r', '\n');

or like this:

string[] rows2 = AllData.Split( System.Environment.NewLine.ToCharArray() );

The question is, do you really want to split them separately ?
Splitting them together is an equally bad idea.
I'd recommend to split only after you've normalized line endings, because Unix/Linux/Mac (POSIX in general) don't use \\r\\n, they only use \\n:

string AllData = @"I'm
a 
multi-line
string";

AllData = AllData.Replace("\r\n", "\n");
string[] rows = AllData.Split('\n');

If you don't normalize, you won't get lines if the string was composed on Linux/Mac.

string[] rows = AllData.Split(new string[] { "\r\n" }, System.StringSplitOptions.RemoveEmptyEntries);

Alternatively, you can split both, and remove empty entries:

string[] rows = AllData.Split(new char[] { '\r', '\n' }, System.StringSplitOptions.RemoveEmptyEntries);

Depends a little bit on what you are trying to achieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM