首页 文章

从String中删除所有空格的有效方法?

提问于
浏览
294

我'm calling a REST API and am receiving an XML response back. It returns a list of a workspace names, and I'正在写一个快速的 IsExistingWorkspace() 方法 . 由于所有工作空间都包含没有空格的连续字符,因此我假设最简单的方法是查找列表中的特定工作空间是否删除所有空格(包括换行符)并执行此操作(XML是从Web接收的字符串请求):

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

我知道它区分大小写,我依靠它 . 我只需要一种方法来有效地删除字符串中的所有空格 . 我知道RegEx和LINQ可以做到,但我对其他想法持开放态度 . 我大多只关心速度 .

16 回答

  • 22

    这是我所知道的最快的方式,即使你说你不想使用正则表达式:

    Regex.Replace(XML, @"\s+", "")
    
  • 73

    我有一种没有正则表达式的替代方法,它似乎表现得相当不错 . 这是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秒 .

  • 3

    在C#中尝试字符串的replace方法 .

    XML.Replace(" ", string.Empty);
    
  • -2

    我的解决方案是使用Split和Join,它的速度非常快,实际上是这里最快的答案 .

    str = string.Join("", str.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
    

    对于带有空格的简单字符串的10,000循环计时,包括新行和制表符

    • split / join = 60毫秒

    • linq chararray = 94毫秒

    • 正则表达式= 437毫秒

    通过将其包装在方法中来改善它,使其具有意义,并且在我们处理它时也使它成为一种扩展方法......

    public static string RemoveWhitespace(this string str) {
        return string.Join("", str.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
    }
    
  • 0

    Build 在Henks answer上我已经用他的答案和一些添加的,更优化的方法创建了一些测试方法 . 我发现结果根据输入字符串的大小而不同 . 因此,我测试了两个结果集 . 在最快的方法中,链接源具有更快的方式 . 但是,由于它的特点是不安全,我把它排除在外 .

    Long input string results:

    • InPlaceCharArray:2021 ms(Sunsetquest's answer) - (Original source

    • 字符串阅读器:6082毫秒

    • LINQ使用native char.IsWhitespace:7357 ms

    • LINQ:7746 ms(Henk's answer

    • ForLoop:32320 ms

    • Regex编译:37157 ms

    • 正则表达式:42940毫秒

    Short input string results:

    • InPlaceCharArray:108毫秒(Sunsetquest's answer) - (Original source

    • 字符串阅读器:327毫秒

    • ForLoop:343毫秒

    • LINQ使用native char.IsWhitespace:624 ms

    • LINQ:645ms(Henk's answer

    • 正则表达式编译:1671毫秒

    • 正则表达式:2599毫秒

    Code

    public class RemoveWhitespace
    {
        public static string RemoveStringReader(string input)
        {
            var s = new StringBuilder(input.Length); // (input.Length);
            using (var reader = new StringReader(input))
            {
                int i = 0;
                char c;
                for (; i < input.Length; i++)
                {
                    c = (char)reader.Read();
                    if (!char.IsWhiteSpace(c))
                    {
                        s.Append(c);
                    }
                }
            }
    
            return s.ToString();
        }
    
        public static string RemoveLinqNativeCharIsWhitespace(string input)
        {
            return new string(input.ToCharArray()
                .Where(c => !char.IsWhiteSpace(c))
                .ToArray());
        }
    
        public static string RemoveLinq(string input)
        {
            return new string(input.ToCharArray()
                .Where(c => !Char.IsWhiteSpace(c))
                .ToArray());
        }
    
        public static string RemoveRegex(string input)
        {
            return Regex.Replace(input, @"\s+", "");
        }
    
        private static Regex compiled = new Regex(@"\s+", RegexOptions.Compiled);
        public static string RemoveRegexCompiled(string input)
        {
            return compiled.Replace(input, "");
        }
    
        public static string RemoveForLoop(string input)
        {
            for (int i = input.Length - 1; i >= 0; i--)
            {
                if (char.IsWhiteSpace(input[i]))
                {
                    input = input.Remove(i, 1);
                }
            }
            return input;
        }
    
        public static string RemoveInPlaceCharArray(string input)
        {
            var len = input.Length;
            var src = input.ToCharArray();
            int dstIdx = 0;
            for (int i = 0; i < len; i++)
            {
                var ch = src[i];
                switch (ch)
                {
                    case '\u0020':
                    case '\u00A0':
                    case '\u1680':
                    case '\u2000':
                    case '\u2001':
                    case '\u2002':
                    case '\u2003':
                    case '\u2004':
                    case '\u2005':
                    case '\u2006':
                    case '\u2007':
                    case '\u2008':
                    case '\u2009':
                    case '\u200A':
                    case '\u202F':
                    case '\u205F':
                    case '\u3000':
                    case '\u2028':
                    case '\u2029':
                    case '\u0009':
                    case '\u000A':
                    case '\u000B':
                    case '\u000C':
                    case '\u000D':
                    case '\u0085':
                        continue;
                    default:
                        src[dstIdx++] = ch;
                        break;
                }
            }
            return new string(src, 0, dstIdx);
        }
    }
    

    Tests

    [TestFixture]
    public class Test
    {
        // Short input
        //private const string input = "123 123 \t 1adc \n 222";
        //private const string expected = "1231231adc222";
    
        // Long input
        private const string input = "123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222";
        private const string expected = "1231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc222";
    
        private const int iterations = 1000000;
    
        [Test]
        public void RemoveInPlaceCharArray()
        {
            string s = null;
            var stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < iterations; i++)
            {
                s = RemoveWhitespace.RemoveInPlaceCharArray(input);
            }
    
            stopwatch.Stop();
            Console.WriteLine("InPlaceCharArray: " + stopwatch.ElapsedMilliseconds + " ms");
            Assert.AreEqual(expected, s);
        }
    
        [Test]
        public void RemoveStringReader()
        {
            string s = null;
            var stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < iterations; i++)
            {
                s = RemoveWhitespace.RemoveStringReader(input);
            }
    
            stopwatch.Stop();
            Console.WriteLine("String reader: " + stopwatch.ElapsedMilliseconds + " ms");
            Assert.AreEqual(expected, s);
        }
    
        [Test]
        public void RemoveLinqNativeCharIsWhitespace()
        {
            string s = null;
            var stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < iterations; i++)
            {
                s = RemoveWhitespace.RemoveLinqNativeCharIsWhitespace(input);
            }
    
            stopwatch.Stop();
            Console.WriteLine("LINQ using native char.IsWhitespace: " + stopwatch.ElapsedMilliseconds + " ms");
            Assert.AreEqual(expected, s);
        }
    
        [Test]
        public void RemoveLinq()
        {
            string s = null;
            var stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < iterations; i++)
            {
                s = RemoveWhitespace.RemoveLinq(input);
            }
    
            stopwatch.Stop();
            Console.WriteLine("LINQ: " + stopwatch.ElapsedMilliseconds + " ms");
            Assert.AreEqual(expected, s);
        }
    
        [Test]
        public void RemoveRegex()
        {
            string s = null;
            var stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < iterations; i++)
            {
                s = RemoveWhitespace.RemoveRegex(input);
            }
    
            stopwatch.Stop();
            Console.WriteLine("Regex: " + stopwatch.ElapsedMilliseconds + " ms");
    
            Assert.AreEqual(expected, s);
        }
    
        [Test]
        public void RemoveRegexCompiled()
        {
            string s = null;
            var stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < iterations; i++)
            {
                s = RemoveWhitespace.RemoveRegexCompiled(input);
            }
    
            stopwatch.Stop();
            Console.WriteLine("RegexCompiled: " + stopwatch.ElapsedMilliseconds + " ms");
    
            Assert.AreEqual(expected, s);
        }
    
        [Test]
        public void RemoveForLoop()
        {
            string s = null;
            var stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < iterations; i++)
            {
                s = RemoveWhitespace.RemoveForLoop(input);
            }
    
            stopwatch.Stop();
            Console.WriteLine("ForLoop: " + stopwatch.ElapsedMilliseconds + " ms");
    
            Assert.AreEqual(expected, s);
        }
    }
    
  • 52

    只是一个替代,因为它看起来很不错:) - 注意:Henks answer是这些中最快的 .

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

    "This is a simple Test" 上测试1,000,000个循环

    此方法= 1.74秒
    正则表达式= 2.58秒
    new String (Henks)= 0.82

  • 1

    我在Felipe Machado的CodeProject上找到a nice write-up on this(在Richard Robertson的帮助下)

    他测试了十种不同的方法 . 这是最快的 unsafe 版本......

    public static unsafe string TrimAllWithStringInplace(string str) {
        fixed (char* pfixed = str) {
            char* dst = pfixed;
            for (char* p = pfixed; *p != 0; p++)
    
                switch (*p) {
    
                    case '\u0020': case '\u00A0': case '\u1680': case '\u2000': case '\u2001':
    
                    case '\u2002': case '\u2003': case '\u2004': case '\u2005': case '\u2006':
    
                    case '\u2007': case '\u2008': case '\u2009': case '\u200A': case '\u202F':
    
                    case '\u205F': case '\u3000': case '\u2028': case '\u2029': case '\u0009':
    
                    case '\u000A': case '\u000B': case '\u000C': case '\u000D': case '\u0085':
                        continue;
    
                    default:
                        *dst++ = *p;
                        break;
        }
    
        return new string(pfixed, 0, (int)(dst - pfixed));
    }
    

    最快的 safe 版......

    public static string TrimAllWithInplaceCharArray(string str) {
    
        var len = str.Length;
        var src = str.ToCharArray();
        int dstIdx = 0;
    
        for (int i = 0; i < len; i++) {
            var ch = src[i];
    
            switch (ch) {
    
                case '\u0020': case '\u00A0': case '\u1680': case '\u2000': case '\u2001':
    
                case '\u2002': case '\u2003': case '\u2004': case '\u2005': case '\u2006':
    
                case '\u2007': case '\u2008': case '\u2009': case '\u200A': case '\u202F':
    
                case '\u205F': case '\u3000': case '\u2028': case '\u2029': case '\u0009':
    
                case '\u000A': case '\u000B': case '\u000C': case '\u000D': case '\u0085':
                    continue;
    
                default:
                    src[dstIdx++] = ch;
                    break;
            }
        }
        return new string(src, 0, dstIdx);
    }
    

    Stian Standahl的Stack Overflow还有一些不错的independent benchmarks,它也展示了Felipe的功能如何比下一个最快的功能快300% .

  • 0

    如果您需要出色的性能,在这种情况下应避免使用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);
        }
    
  • 490

    正则表达式过度;只需在字符串上使用扩展名(感谢Henk) . 这是微不足道的,应该成为框架的一部分 . 无论如何,这是我的实现:

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

    这是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();
    }
    
  • 26

    我需要用空格替换字符串中的空格,而不是重复空格 . 例如,我需要转换如下内容:

    "a b   c\r\n d\t\t\t e"
    

    "a b c d e"
    

    我使用以下方法

    private static string RemoveWhiteSpace(string value)
    {
        if (value == null) { return null; }
        var sb = new StringBuilder();
    
        var lastCharWs = false;
        foreach (var c in value)
        {
            if (char.IsWhiteSpace(c))
            {
                if (lastCharWs) { continue; }
                sb.Append(' ');
                lastCharWs = true;
            }
            else
            {
                sb.Append(c);
                lastCharWs = false;
            }
        }
        return sb.ToString();
    }
    
  • 15

    我假设您的XML响应如下所示:

    var xml = @"<names>
                    <name>
                        foo
                    </name>
                    <name>
                        bar
                    </name>
                </names>";
    

    处理XML的最佳方法是使用XML解析器,例如 LINQ to XML

    var doc = XDocument.Parse(xml);
    
    var containsFoo = doc.Root
                         .Elements("name")
                         .Any(e => ((string)e).Trim() == "foo");
    
  • 13

    这是另一种变体:

    public static string RemoveAllWhitespace(string aString)
    {
      return String.Join(String.Empty, aString.Where(aChar => aChar !Char.IsWhiteSpace(aChar)));
    }
    

    与大多数其他解决方案一样,我没有进行详尽的基准测试,但这对我的目的来说效果很好 .

  • 7

    我发现不同的结果是真实的 . 我试图用一个空格替换所有空格,正则表达式非常慢 .

    return( Regex::Replace( text, L"\s+", L" " ) );
    

    对我来说最有效的方法(在C cli中)是:

    String^ ReduceWhitespace( String^ text )
    {
      String^ newText;
      bool    inWhitespace = false;
      Int32   posStart = 0;
      Int32   pos      = 0;
      for( pos = 0; pos < text->Length; ++pos )
      {
        wchar_t cc = text[pos];
        if( Char::IsWhiteSpace( cc ) )
        {
          if( !inWhitespace )
          {
            if( pos > posStart ) newText += text->Substring( posStart, pos - posStart );
            inWhitespace = true;
            newText += L' ';
          }
          posStart = pos + 1;
        }
        else
        {
          if( inWhitespace )
          {
            inWhitespace = false;
            posStart = pos;
          }
        }
      }
    
      if( pos > posStart ) newText += text->Substring( posStart, pos - posStart );
    
      return( newText );
    }
    

    我首先通过单独替换每个字符来尝试上述例程,但是必须切换到为非空格部分执行子字符串 . 申请1,200,000个字符串时:

    • 以上例程在25秒内完成

    • 以上例程在95秒内单独替换字符

    • 正则表达式在15之后中止分钟 .

  • 4

    我们可以用:

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

    我们可以使用System.Linq,我们可以在一行中完成:

    string text = "My text with white spaces...";
    text = new string(text.ToList().Where(c => c != ' ').ToArray());
    

相关问题