3

I am removing values with less than 8 characters from an array, but empty strings still remain. How to get rid of them?

for (int i = 0; i < reportbOR.Length; i++)
{
    border = "border:" +reportbOR[i].colorborder;
    string[] text_arr = border.Split('\n');

    foreach (var item in text_arr)
    {
        if (item.Length < 8)
            border = border.Replace(item, "");
    }
}
4
  • 2
    This is easier to do in a List. If performance is not an issue, convert it to a list, remove the items you want from the list, and put the result back into an array. Or, just use a List from the start. Commented Jun 14, 2022 at 21:17
  • Otherwise, you'll wind up copying out everything you want to keep into a new array anyway. Commented Jun 14, 2022 at 21:17
  • 4
    You can also remove empty records immediately from the Split function: border.Split('\n', StringSplitOptions.RemoveEmptyEntries) Commented Jun 14, 2022 at 21:19
  • What will you do with this array after the loop? Right now, text_arr goes out of scope after the outer for loop, and this won't accomplish anything. Looking at this, I can probably get it down to a single line of code, iff I know more about how this data will be used. Commented Jun 14, 2022 at 22:06

4 Answers 4

2

You can use the overload of string.Split which accepts a StringSplitOptions enum:

string[] text_arr = border.Split(new string[] { "\n" },
  StringSplitOptions.RemoveEmptyEntries);

Since it's unclear as to when you are having issues removing the empty entries, and you are also replacing strings with 8 or less characters with empty strings, you can use Linq:

text_arr = text_arr.Where(a => !string.IsNullOrWhitespace(a)).ToArray();

You can read about the overload here: stringsplitoptions

And here: string.split

EDIT Here it is in one line:

string[] text_arr = border.Split(new string[] { "\n" }, 
                    StringSplitOptions.RemoveEmptyEntries)
                    .Where(a => !string.IsNullOrWhitespace(a) && a.Length >= 8)
                    .ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

I tried your suggestions but, alas, empty lines still remain
@JOHNSMITH Then you'll need to add some example data to your post. This should work.
1

You could filter out null, white space, and character length with a couple of different approaches. We will start similar to your above code.

var border = new List<string>();
for(var index = 0; index < reportbOR.Length; index++)
      if(!String.IsNullOrEmpty(reportbOR[index].colorBorder) && reportbOR[index].colorBorder.Length < 8)
           border.Add($"border: {reportbOR[index].colorBorder}");

I won't mention the StringSplitOptions because of @NSevens response, the documentation is pretty self explanatory but could be applicable.

One thing to denote, if you use LINQ on a String it will parse into a character array. But if you use your root object you could leverage LINQ.

var borders = reportbOR
      .Where(property => !String.IsNullOrEmpty(property.colorBorder) && property.colorBorder < 8))
      .Select(property => new $"border: {property}");

Also, it would be important to know that if you only care about null or empty, then use IsNullOrEmpty. If you want to sanitize for white-space and white-space characters then use IsNullOrWhiteSpace.

Comments

0

Something like this?

for (int i = 0; i < reportbOR.Length; i++)
{
    var strings = $"border:{reportbOR[i].colorborder}"
        .Split(Environment.NewLine)
        .Where(str => str.Length > 8);

    border = string.Join(Environment.NewLine, strings);
}

Comments

0

try this

list = list.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.