我正在调用一个REST API,并收到一个XML响应。它返回一个工作区名称列表,我正在编写一个快速的IsExistingWorkspace()方法。因为所有的工作空间都是由没有空格的连续字符组成的,我假设找出特定工作空间是否在列表中最简单的方法是删除所有空格(包括换行符)并这样做(XML是从web请求接收到的字符串):

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

我知道这是区分大小写的,我就靠这一点了。我只是需要一种方法来有效地删除字符串中的所有空白。我知道RegEx和LINQ可以做到,但我对其他想法持开放态度。我最关心的是速度。


当前回答

我有一种没有regexp的替代方法,它的性能似乎相当不错。这是Brandon Moretz回答的延续:

 public static string RemoveWhitespace(this string input)
 {
    return new string(input.ToCharArray()
        .Where(c => !Char.IsWhiteSpace(c))
        .ToArray());
 }

我在一个简单的单元测试中测试了它:

[Test]
[TestCase("123 123 1adc \n 222", "1231231adc222")]
public void RemoveWhiteSpace1(string input, string expected)
{
    string s = null;
    for (int i = 0; i < 1000000; i++)
    {
        s = input.RemoveWhitespace();
    }
    Assert.AreEqual(expected, s);
}

[Test]
[TestCase("123 123 1adc \n 222", "1231231adc222")]
public void RemoveWhiteSpace2(string input, string expected)
{
    string s = null;
    for (int i = 0; i < 1000000; i++)
    {
        s = Regex.Replace(input, @"\s+", "");
    }
    Assert.AreEqual(expected, s);
}

对于1,000,000次尝试,第一个选项(不带regexp)的运行时间不到一秒(在我的机器上是700毫秒),第二个需要3.5秒。

其他回答

如果需要出色的性能,在这种情况下应该避免使用LINQ和正则表达式。我做了一些性能基准测试,似乎如果你想从字符串的开头和结尾去除空白,string. trim()是你的最终函数。

如果你需要从字符串中去除所有的空格,下面的方法是最快的:

    public static string RemoveWhitespace(this string input)
    {
        int j = 0, inputlen = input.Length;
        char[] newarr = new char[inputlen];

        for (int i = 0; i < inputlen; ++i)
        {
            char tmp = input[i];

            if (!char.IsWhiteSpace(tmp))
            {
                newarr[j] = tmp;
                ++j;
            }
        }
        return new String(newarr, 0, j);
    }

只是一个选择,因为它看起来很不错:)-注:Henks的回答是这些回答中最快的。

input.ToCharArray()
 .Where(c => !Char.IsWhiteSpace(c))
 .Select(c => c.ToString())
 .Aggregate((a, b) => a + b);

在“这是一个简单的测试”上测试1,000,000次循环

这个方法= 1.74秒 Regex = 2.58秒 new String (Henks) = 0.82秒

下面是RegEx解决方案的一个简单的线性替代方案。我不知道哪个更快;你必须对它进行基准测试。

static string RemoveWhitespace(string input)
{
    StringBuilder output = new StringBuilder(input.Length);

    for (int index = 0; index < input.Length; index++)
    {
        if (!Char.IsWhiteSpace(input, index))
        {
            output.Append(input[index]);
        }
    }
    return output.ToString();
}

我有一种没有regexp的替代方法,它的性能似乎相当不错。这是Brandon Moretz回答的延续:

 public static string RemoveWhitespace(this string input)
 {
    return new string(input.ToCharArray()
        .Where(c => !Char.IsWhiteSpace(c))
        .ToArray());
 }

我在一个简单的单元测试中测试了它:

[Test]
[TestCase("123 123 1adc \n 222", "1231231adc222")]
public void RemoveWhiteSpace1(string input, string expected)
{
    string s = null;
    for (int i = 0; i < 1000000; i++)
    {
        s = input.RemoveWhitespace();
    }
    Assert.AreEqual(expected, s);
}

[Test]
[TestCase("123 123 1adc \n 222", "1231231adc222")]
public void RemoveWhiteSpace2(string input, string expected)
{
    string s = null;
    for (int i = 0; i < 1000000; i++)
    {
        s = Regex.Replace(input, @"\s+", "");
    }
    Assert.AreEqual(expected, s);
}

对于1,000,000次尝试,第一个选项(不带regexp)的运行时间不到一秒(在我的机器上是700毫秒),第二个需要3.5秒。

Regex太夸张了;只是在字符串上使用扩展(感谢Henk)。这是微不足道的,应该是框架的一部分。总之,这是我的实现:

public static partial class Extension
{
    public static string RemoveWhiteSpace(this string self)
    {
        return new string(self.Where(c => !Char.IsWhiteSpace(c)).ToArray());
    }
}