Corey Ogburn Corey Ogburn - 10 months ago 46
C# Question

Efficient way to remove ALL whitespace from String?

I'm calling a REST API and receiving an XML response back. It returns a list of a workspace names and I'm writing a quick

method. Since all workspaces consist of contiguous characters with no whitespace, I'm assuming the easiest way to find out if a particular workspace is in the list is to remove all whitespace (including newlines) and doing this (XML is the string received from the web request):

XML.Contains("<name>" + workspaceName + "</name>");

I know it's case-sensitive and I'm relying on that. I just need a way to remove all whitespace in a string efficiently. I know RegEx and LINQ can do it, but I'm open to other ideas. Mostly just concerned about speed.

Answer Source
Regex.Replace(XML, @"\s+", "")

Fastest way I know of, even though you said you didn't want to use Regular Expressions.