C# 如何用一个空格替换多个空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1279859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to replace multiple white spaces with one white space
提问by Matt
Let's say I have a string such as:
假设我有一个字符串,例如:
"Hello how are you doing?"
I would like a function that turns multiple spaces into one space.
我想要一个将多个空间变成一个空间的函数。
So I would get:
所以我会得到:
"Hello how are you doing?"
I know I could use regex or call
我知道我可以使用正则表达式或致电
string s = "Hello how are you doing?".replace(" "," ");
But I would have to call it multiple times to make sure all sequential whitespaces are replaced with only one.
但是我必须多次调用它以确保所有连续的空格都只替换为一个。
Is there already a built in method for this?
是否已经有一个内置的方法?
采纳答案by Tim Hoolihan
string cleanedString = System.Text.RegularExpressions.Regex.Replace(dirtyString,@"\s+"," ");
回答by Brandon
A regular expressoin would be the easiest way. If you write the regex the correct way, you wont need multiple calls.
正则表达式将是最简单的方法。如果您以正确的方式编写正则表达式,则不需要多次调用。
Change it to this:
改成这样:
string s = System.Text.RegularExpressions.Regex.Replace(s, @"\s{2,}", " ");
回答by Michael D.
Regex regex = new Regex(@"\W+");
string outputString = regex.Replace(inputString, " ");
回答by Scott Dorman
There is no way built in to do this. You can try this:
没有内置的方法可以做到这一点。你可以试试这个:
private static readonly char[] whitespace = new char[] { ' ', '\n', '\t', '\r', '\f', '\v' };
public static string Normalize(string source)
{
return String.Join(" ", source.Split(whitespace, StringSplitOptions.RemoveEmptyEntries));
}
This will remove leading and trailing whitespce as well as collapse any internal whitespace to a single whitespace character. If you really only want to collapse spaces, then the solutions using a regular expression are better; otherwise this solution is better. (See the analysisdone by Jon Skeet.)
这将删除前导和尾随 whitespce 并将任何内部空白折叠为单个空白字符。如果你真的只想折叠空间,那么使用正则表达式的解决方案会更好;否则这个解决方案更好。(请参阅Jon Skeet 所做的分析。)
回答by Jon Skeet
While the existing answers are fine, I'd like to point out one approach which doesn'twork:
虽然现有的答案很好,但我想指出一种不起作用的方法:
public static string DontUseThisToCollapseSpaces(string text)
{
while (text.IndexOf(" ") != -1)
{
text = text.Replace(" ", " ");
}
return text;
}
This can loop forever. Anyone care to guess why? (I only came across this when it was asked as a newsgroup question a few years ago... someone actually ran into it as a problem.)
这可以永远循环。有人想知道为什么吗?(我只是在几年前作为新闻组问题被问到时才遇到这个问题……实际上有人遇到了这个问题。)
回答by MAK
As already pointed out, this is easily done by a regular expression. I'll just add that you might want to add a .trim() to that to get rid of leading/trailing whitespace.
正如已经指出的,这可以通过正则表达式轻松完成。我将补充一点,您可能想要添加一个 .trim() 以摆脱前导/尾随空格。
回答by Jon Skeet
This question isn't as simple as other posters have made it out to be (and as I originally believed it to be) - because the question isn't quite precise as it needs to be.
这个问题不像其他海报那样简单(正如我最初认为的那样) - 因为这个问题并不像它需要的那样精确。
There's a difference between "space" and "whitespace". If you onlymean spaces, then you should use a regex of " {2,}"
. If you mean anywhitespace, that's a different matter. Should allwhitespace be converted to spaces? What should happen to space at the start and end?
“空格”和“空格”是有区别的。如果您只表示空格,那么您应该使用" {2,}"
. 如果你的意思是任何空格,那是另一回事。是否应该将所有空格都转换为空格?空间在开始和结束时会发生什么?
For the benchmark below, I've assumed that you only care about spaces, and you don't want to do anything to single spaces, even at the start and end.
对于下面的基准测试,我假设您只关心空格,并且您不想对单个空格做任何事情,即使在开头和结尾也是如此。
Note that correctness is almost always more important than performance. The fact that the Split/Join solution removes any leading/trailing whitespace (even just single spaces) is incorrect as far as your specified requirements (which may be incomplete, of course).
请注意,正确性几乎总是比性能更重要。Split/Join 解决方案删除任何前导/尾随空格(甚至只是单个空格)这一事实就您指定的要求而言是不正确的(当然,这可能不完整)。
The benchmark uses MiniBench.
基准测试使用MiniBench。
using System;
using System.Text.RegularExpressions;
using MiniBench;
internal class Program
{
public static void Main(string[] args)
{
int size = int.Parse(args[0]);
int gapBetweenExtraSpaces = int.Parse(args[1]);
char[] chars = new char[size];
for (int i=0; i < size/2; i += 2)
{
// Make sure there actually *is* something to do
chars[i*2] = (i % gapBetweenExtraSpaces == 1) ? ' ' : 'x';
chars[i*2 + 1] = ' ';
}
// Just to make sure we don't have a c:\Users\Jon\Test>test 1000 50
============ Normalize ============
NormalizeWithSplitAndJoin 1159091 0:30.258 22.93
NormalizeWithRegex 26378882 0:30.025 1.00
c:\Users\Jon\Test>test 1000 5
============ Normalize ============
NormalizeWithSplitAndJoin 947540 0:30.013 1.07
NormalizeWithRegex 1003862 0:29.610 1.00
c:\Users\Jon\Test>test 1000 1001
============ Normalize ============
NormalizeWithSplitAndJoin 1156299 0:29.898 21.99
NormalizeWithRegex 23243802 0:27.335 1.00
at the end
// for odd sizes
chars[chars.Length-1] = 'y';
string bigString = new string(chars);
// Assume that one form works :)
string normalized = NormalizeWithSplitAndJoin(bigString);
var suite = new TestSuite<string, string>("Normalize")
.Plus(NormalizeWithSplitAndJoin)
.Plus(NormalizeWithRegex)
.RunTests(bigString, normalized);
suite.Display(ResultColumns.All, suite.FindBest());
}
private static readonly Regex MultipleSpaces =
new Regex(@" {2,}", RegexOptions.Compiled);
static string NormalizeWithRegex(string input)
{
return MultipleSpaces.Replace(input, " ");
}
// Guessing as the post doesn't specify what to use
private static readonly char[] Whitespace =
new char[] { ' ' };
static string NormalizeWithSplitAndJoin(string input)
{
string[] split = input.Split
(Whitespace, StringSplitOptions.RemoveEmptyEntries);
return string.Join(" ", split);
}
}
A few test runs:
几个测试运行:
public static string NormalizeWhiteSpace(string S)
{
string s = S.Trim();
bool iswhite = false;
int iwhite;
int sLength = s.Length;
StringBuilder sb = new StringBuilder(sLength);
foreach(char c in s.ToCharArray())
{
if(Char.IsWhiteSpace(c))
{
if (iswhite)
{
//Continuing whitespace ignore it.
continue;
}
else
{
//New WhiteSpace
//Replace whitespace with a single space.
sb.Append(" ");
//Set iswhite to True and any following whitespace will be ignored
iswhite = true;
}
}
else
{
sb.Append(c.ToString());
//reset iswhitespace to false
iswhite = false;
}
}
return sb.ToString();
}
Here the first number is the number of iterations, the second is the time taken, and the third is a scaled score with 1.0 being the best.
这里第一个数字是迭代次数,第二个数字是花费的时间,第三个数字是比例分数,1.0 是最好的。
That shows that in at least some cases (including this one) a regular expression canoutperform the Split/Join solution, sometimes by a very significant margin.
这表明至少在某些情况下(包括这种情况),正则表达式可以胜过 Split/Join 解决方案,有时优势非常明显。
However, if you change to an "all whitespace" requirement, then Split/Join doesappear to win. As is so often the case, the devil is in the detail...
但是,如果您更改为“全空白”要求,那么拆分/连接似乎确实会获胜。通常情况下,细节决定成败……
回答by Jon Skeet
Smallest solution:
最小的解决方案:
var regExp=/\s+/g, newString=oldString.replace(regExp,' ');
var regExp=/\s+/g, newString=oldString.replace(regExp,' ');
回答by user214147
I'm sharing what I use, because it appears I've come up with something different. I've been using this for a while and it is fast enough for me. I'm not sure how it stacks up against the others. I uses it in a delimited file writer and run large datatables one field at a time through it.
我正在分享我使用的东西,因为看起来我想出了一些不同的东西。我已经使用了一段时间,它对我来说已经足够快了。我不确定它与其他人相比如何。我在分隔文件编写器中使用它,并通过它一次一个字段运行大型数据表。
Linha.Split(" ").ToList().Where(Function(x) x <> " ").ToArray
回答by Patryk Moura
VB.NET
网络
Linha.Split(" ").ToList().Where(x => x != " ").ToArray();
C#
C#
##代码##Enjoy the power of LINQ =D
享受LINQ的力量=D