如何在C#中添加换行符 [英] How do add a line break in C#
问题描述
我正在建立的网络表单背后有一些逻辑。逻辑处理用户是否为必填字段输入了有效值。代码如下:
I have some logic behind a web form I'm building. The logic handles whether a user entered a valid value for required field. Code below:
var operationResult = new BO.OperationResult();
// Physical Location Required
if (physicalLocationDesc.Trim().Length <= 0)
{
operationResult.Successful = false;
operationResult.Message += "A location is required as part of the entry";
}
// Type of Connection Desc. Required
if (typeOfConnectionID.Trim().Length <= 0)
{
operationResult.Successful = false;
operationResult.Message += "A description for the connection type is required.";
}
// Password change frequency desc. required
if (passwordChangeFrequency.Trim().Length <= 0)
{
operationResult.Successful = false;
operationResult.Message += "Please enter a description for the Password Change Frequency";
}
我遇到的问题是operationResult.Message正在返回:
作为条目的一部分需要一个位置需要连接类型的描述。请输入密码更改频率的说明
我想在每条错误消息后添加换行符。关于如何解决这个问题的任何想法? (请注意,如果未输入1个字段但上面的代码可以正常工作但是更难以读取2个或更多字段。)
The issue that I'm having is that the operationResult.Message is returning:
"A location is required as part of the entryA description for the connection type is required.Please enter a description for the Password Change Frequency"
I'd like to add a line break after each individual error message. Any ideas on how to go about this? (Please note the code above works fine if 1 field is not entered but becomes harder to read 2 or more fields are not entered.)
推荐答案
使用Environment.NewLine方法:
http://www.dotnetperls.com/newline [ ^ ]
Use the Environment.NewLine method:
http://www.dotnetperls.com/newline[^]
System.Envirement.NewLine是您的解决方案。参见示例:
http://www.dotnetperls.com/newline [ ^ ]
System.Envirement.NewLine is your solution. See example:
http://www.dotnetperls.com/newline[^]
两种方式:
1.尽可能将单个邮件分开,然后在结尾将它们组合一次:
Two ways:
1. Keep the individual message separate as long as possible and then combine them once at "the end":
var operationResult = new BO.OperationResult();
List<string> messages = new List<string>();
// Physical Location Required
if (string.IsNullOrWhiteSpace(physicalLocationDesc))
{
operationResult.Successful = false;
messages.Add("A location is required as part of the entry");
}
// Type of Connection Desc. Required
if (string.IsNullOrWhiteSpace(typeOfConnectionID))
{
operationResult.Successful = false;
messages.Add("A description for the connection type is required.");
}
// Password change frequency desc. required
if (string.IsNullOrWhiteSpace(passwordChangeFrequency))
{
operationResult.Successful = false;
messages.Add("Please enter a description for the Password Change Frequency");
}
// etc...
operationResult.Message = string.Join(Environment.NewLine, messages);
2.使用 StringBuilder
:
2. Use a StringBuilder
:
var operationResult = new BO.OperationResult();
StringBuilder messages = new StringBuilder();
string sep = string.Empty;
// Physical Location Required
if (string.IsNullOrWhiteSpace(physicalLocationDesc))
{
operationResult.Successful = false;
messages.Append(sep).Append("A location is required as part of the entry");
sep = Environment.NewLine;
}
// Type of Connection Desc. Required
if (string.IsNullOrWhiteSpace(typeOfConnectionID))
{
operationResult.Successful = false;
messages.Append(sep).Append("A description for the connection type is required.");
sep = Environment.NewLine;
}
// Password change frequency desc. required
if (string.IsNullOrWhiteSpace(passwordChangeFrequency))
{
operationResult.Successful = false;
messages.Append(sep).Append("Please enter a description for the Password Change Frequency");
sep = Environment.NewLine;
}
// etc...
operationResult.Message = messages.ToString();
这篇关于如何在C#中添加换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!