有效的方法去除字符串中的所有空白? [英] Efficient way to remove ALL whitespace from String?

查看:101
本文介绍了有效的方法去除字符串中的所有空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调用REST API和接收XML响应回来。它返回一个工作区的名称列表,我正在写一个快速的 IsExistingWorkspace()方法。由于所有的工作区由连续的字符,不能有空格,我假设的最简单的方法找出如果一个特定的工作区是在列表中是删除所有空格(包括新行),做这个(XML是从网上收到的字​​符串要求):

  XML.Contains(<名称>+工作区名+< /名称>);

我知道这是区分大小写的,我依赖上。我只是需要一种方法来有效地去除所有空格中的字符串。我知道,正则表达式和LINQ可以做到这一点,但我开到其他的想法。大多只是关心速度。


解决方案

  Regex.Replace(XML,@\\ S +,)

我知道的最快的途径,即使你说你不希望使用普通防爆pressions。

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 IsExistingWorkspace() 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.

解决方案

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

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

这篇关于有效的方法去除字符串中的所有空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆