简体   繁体   English

如何将VB.net常量转换为C#?

[英]How to convert VB.net Constants to C#?

I am converting VB.Net project to C# in VS2013. 我正在将VS2013中的VB.Net项目转换为C#。 I come across a problem in converting constants in VB.NET to C# code and don't want to use the refernce using Microsoft.VisualBasic; 我在将VB.NET中的常量转换为C#代码时遇到了一个问题,并且不想通过Microsoft.VisualBasic使用引用 in my code. 在我的代码中。 Please suggest me some links for this. 请为我建议一些链接。 check the examples below 检查以下示例

Examples: 例子:

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

My Code 我的密码

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. C#中不需要这样的常量,因为它们是字符文字: \\r\\n

Environment.Newline isn't really CRLF ( \\r\\n in C#) - it's environment dependent. Environment.Newline并不是真正的 CRLF(在C#中为\\r\\n )-它取决于环境。 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 . 只有您知道"\\r\\n"还是Environment.NewLine是更好的选择-如果您的输入数据取决于环境,请使用Environment.NewLine If it's supposed to always be "\\r\\n" , use that. 如果应该始终为"\\r\\n" ,请使用该名称。

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: 我建议仅在对行尾进行标准化后才进行拆分,因为Unix / Linux / Mac(通常为POSIX)不使用\\ r \\ n,它们仅使用\\ 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. 如果不规范化,则如果字符串是在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. 取决于您要实现的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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