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

DonBaechtel-7903 141 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 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,420 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,265 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,515 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,099 questions
{count} votes

Accepted answer
  1. P a u l 10,401 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