Click here to Skip to main content
15,669,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi there,
I have an M3U file and it's usually formatted likes this:

#EXTM3U

#EXTINF:-1, tvg-name="NAME 1" tvg-logo="LOGO" group-title="TITLE 1",ITEM 1
https://www.SampleSite1.com


#EXTINF:-1, tvg-name="NAME 2" tvg-logo="LOGO" group-title="TITLE 2",ITEM 2
https://www.SampleSite2.com




#EXTINF:-1, tvg-name="NAME 3" tvg-logo="LOGO" group-title="TITLE 3",ITEM 3
https://www.SampleSite3.com

#EXTINF:-1, tvg-name="NAME 4" tvg-logo="LOGO" group-title="TITLE 4",ITEM 4
https://www.SampleSite4.com


Number of empty lines mostly are not the same. I'm going to split these Items and remove spaces between them to have an item (without spaces) on each array. What's the best way to do it using String or regex?

What I have tried:

string[] separatingStrings = { "#EXTINF", "..." };

allItems = text.Split(separatingStrings, System.StringSplitOptions.RemoveEmptyEntries);<pre>


I've used this code, but "#EXTINF" is removed from arrays and empty lines are also included.
Posted
Updated 9-Oct-22 18:38pm
v4

A newline isn't an empty entry because any empty entry is an empty string: one which contains no characters at all. Newline is a character (or two characters on some systems) so it doesn't get removed.
Try adding a newline to your separating strings collection.
 
Share this answer
 
I prefer use LINQ for this

var objects = myString.Split(',')
.Where(x => !string.IsNullOrEmpty(x))
.ToList();
 
Share this answer
 
Comments
Richard Deeming 11-Oct-22 8:50am    
That's particularly inefficient. Even with a tiny input string, a quick benchmark shows it takes over twice as long as RemoveEmptyEntries in .NET 6, and over 2.5x as long in .NET Framework. It also allocates almost 1.5x the memory.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900