AboutSQL Server, Analytics, .Net, Machine Learning, R, Python Archives
About Me
Mitch Wheat has been working as a professional programmer since 1984, graduating with a honours degree in Mathematics from Warwick University, UK in 1986. He moved to Perth in 1995, having worked in software houses in London and Rotterdam. He has worked in the areas of mining, electronics, research, defence, financial, GIS, telecommunications, engineering, and information management. Mitch has worked mainly with Microsoft technologies (since Windows version 3.0) but has also used UNIX. He holds the following Microsoft certifications: MCPD (Web and Windows) using C# and SQL Server MCITP (Admin and Developer). His preferred development environment is C#, .Net Framework and SQL Server. Mitch has worked as an independent consultant for the last 10 years, and is currently involved with helping teams improve their Software Development Life Cycle. His areas of special interest lie in performance tuning |
Sunday, November 02, 2008String.Split(): Skip Empty Entries
At the risk of publicising that I'm the last person to know this(!), I recently discovered that String.Split() has an overload that takes a parameter
StringSplitOptions.RemoveEmptyEntries that does exactly what it says, like this: char[] separator = new char[] { ',' }; string[] result; string toSplit = "Rick,Dave,,Nick,,,Roger,"; result = toSplit.Split(separator, StringSplitOptions.RemoveEmptyEntries); foreach (string s in result) { Console.WriteLine("[{0}]", s); } This is also very useful for splitting text where extra whitespace should be ignored: string woods = "The woods are lovely, dark and deep.." + "But I have promises to keep, " + "And miles to go before I sleep,, " + "And miles to go before I sleep."; char[] whitespace = { ' ', ',', ';', ':', '.', '!', '?' }; string[] words = woods.Split(whitespace, StringSplitOptions.RemoveEmptyEntries); foreach (string s in words) { Console.WriteLine("[{0}]", s); } |
ContactMSN, Email: mitch døt wheat at gmail.com LinksFavorites
Blogs |