سؤال

I am working with strings in C#. I have a string like,

StringBuilder sb = new StringBuilder();
sb.AppendLine(ValidatorMethod1());
sb.AppendLine(ValidatorMethod2());

after "sb" is called on ValidatorMethod1(), I call a ValidatorMethod2() as shown, both returns a error message as a string based on a condition. If ValidatorMethod1() and ValidatorMethod2() returns a error msg, then all is fine. But if ValidatorMethod2() fails, then the length of the error message returned from ValidatorMethod2() is "0", but still a line is appended with "sb" and thus a empty error line is appended after ValidatorMethod1()'s error msg.

I tried googling, but links like:

So please can anyone give a idea to, "put the returned string in newline if its length is greater than zero" or do nothing?

EDIT:

Hi All, I guess I dint get the proper solution for which I am lookin for. I do not want to append line after each "sb". But if I have a error msg, then, I want to put in NEWLINE.. Can someone give me a different solution which caters to my need?..

هل كانت مفيدة؟

المحلول

You can us String.IsNullOrEmpty to see if the result has a length greater than 0

For example:

string result = ValidatorMethod2();
if (!String.IsNullOrEmpty(result))
   sb.AppendLine(result);

نصائح أخرى

Any reason you're not just checking that yourself? This will hardly add any noticeable overhead:

StringBuilder sb = new StringBuilder();

String msg;

msg = ValidatorMethod1();
if (msg.Length > 0)
    sb.AppendLine(msg);

msg = ValidatorMethod2();
if (msg.Length > 0)
    sb.AppendLine(msg);

In fact, there's no faster way. Even if there'd be some built-in functionality provided by Microsoft, it wouldn't do something significantly different.

You can use this code for each validator:

string error = null;

// repeat for each validator
error = ValidatorMethod1();
if (!String.IsNullOrEmpty(error)) sb.AppendLine(error);

You can i.e. do:

string sb1 = ValidatorMethod1();
string sb2 = ValidatorMethod2();
if (sb1 != "") sb.AppendLine(sb1);
if (sb2 != "") sb.AppendLine(sb2);

You may try doing something like that (calling AppendLine before each non-empty string if sb contains any text):

StringBuilder sb = new StringBuilder();

String message = ValidatorMethod1();

if (!String.IsNullOrEmpty(message))
  sb.Append(message);

message = ValidatorMethod2();

if (!String.IsNullOrEmpty(message)) {
  if (sb.Length > 0)
    sb.AppendLine();

  sb.Append(message);
}

in general case, when you have many validators:

StringBuilder sb = new StringBuilder();

foreach(String result in getValidatorsResults()) {
  if (String.IsNullOrEmpty(result)) 
    continue;

  if (sb.Length > 0)
    sb.AppendLine();

  sb.Append(result);
}

In case you are working with a collection of validation methods, you might want to consider something like this:

var validationMethods = new Func<string>[]
{
    () => ValidationMethod1(),
    () => ValidationMethod2(),
    // Add more validtion methods here
};
string error = string.Join(Environment.NewLine, validationMethods.Select(validationMethod => validationMethod()).Where(message=> !string.IsNullOrEmpty(message)));

This solution is easily scaleable if you are need to change the number of validation methods you want to run in a sequence, or when you even want to have a dynamic number of validation methods (then you should use something like List<Func<string>> instead).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top