C# String.Split with TrimEntries option returbs zero length entries

DonBaechtel-7903 191 Reputation points
2023-04-01T21:42:19.3633333+00:00

The following code reads in the file OK. It splits the text into several lines without end-of-line terminators OK. But even though the Split has the TrimEntries option, it still returns several lines of zero length. Why? How to remove zero-length entries with Split? Is this another MS bug?

                    char[] charSeparators = new char[] { '\x0A', '\x0C', '\x0D' };
                    using (StreamReader sr = new StreamReader(fileNm))
                    {
                        content = sr.ReadToEnd();
                        linesIn = content.Split(charSeparators, 
                            StringSplitOptions.RemoveEmptyEntries
                            & StringSplitOptions.TrimEntries);
                        sr.Close();
                    }

Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | .NET | Other
Developer technologies | Visual Studio | Other
Developer technologies | C#
{count} votes

Accepted answer
  1. P a u l 10,761 Reputation points
    2023-04-01T21:47:34.3433333+00:00

    Just a guess but did you mean to bitwise OR these instead of AND?

    StringSplitOptions.RemoveEmptyEntries & StringSplitOptions.TrimEntries
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.