0

我有一个字符串值

string text = "-----------------\r\n      Some text here      Some text here\r\n"

有没有办法在每个有很多空格的地方删除 2 个空格?

谢谢您的帮助

4

11 回答 11

4

您可以使用正则表达式。

var result = new Regex("\s{2,}").Replace(text,constForWhiteSpace)
于 2013-04-16T10:45:52.330 回答
2

这是你想要的?

text = text.Replace("   "," ");
于 2013-04-16T10:44:14.000 回答
2
Regex regex = new Regex(@"[ ]{2,}");    // Regex to match more than two occurences of space 
text = regex.Replace(text, @"");
于 2013-04-16T10:45:49.393 回答
1

编辑:这是为了速度,否则其他答案建议的正则表达式很好。这个功能非常快。

编辑2:如果你想保留新行,只需删除 \r \n 。

        public static String SingleSpacedTrim(String inString)
        {
            StringBuilder sb = new StringBuilder();
            Boolean inBlanks = false;
            foreach (Char c in inString)
            {
                switch (c)
                {
                    case '\r':
                    case '\n':
                    case '\t':
                    case ' ':
                        if (!inBlanks)
                        {
                            inBlanks = true;
                            sb.Append(' ');
                        }   
                        continue;
                    default:
                        inBlanks = false;
                        sb.Append(c);
                        break;
                }
            }
            return sb.ToString().Trim();
        }
于 2013-04-16T10:44:08.620 回答
1

试试看....

   string _str = "-----------------\r\n      Some text here      Some text here\r\n";
    _str = _str.Trim();
    Console.WriteLine(_str.Replace(" ",""));

输出:
SometexthereSometexthere

于 2013-04-16T10:52:00.223 回答
0

不是最好的代码,但如果你想使用正则表达式,这应该从文本中删除两个空格。

//one.two..three...four....five..... (dots = spaces)
string input = "one two  three   four    five     ";

string result = new Regex(@"\s{2,}").Replace(input, delegate(Match m)
{    
    if (m.Value.Length > 2)
    {    
        int substring = m.Value.Length - 2;
        //if there are two spaces, just remove the one
        if (m.Value.Length == 2) substring = 1;

        string str = m.Value.Substring(m.Value.Length - substring);
        return str;
    }
    return m.Value;
});

输出将是

//dots represent spaces. Two spaces are removed from each one but one 
//unless there are two spaces, in which case only one is removed.
one.two.three.four..five...
于 2013-04-16T11:15:30.247 回答
0

如果你知道有多少个空格,你可以使用

text.Replace("       ", "     ");
于 2013-04-16T10:44:41.407 回答
0
var dirty = "asdjk    asldkjas dasdl l aksdjal;sd            asd;lkjdaslj";

var clean = string.Join(" ", dirty.Split(' ').Where(x => !string.IsNullOrEmpty(x)));
于 2013-04-16T10:52:04.667 回答
0

尝试这个:

var result = new Regex("\s{2,}").Replace(text,string.Empty)
于 2013-04-16T10:49:29.207 回答
0

如果你不喜欢 Regex 或者文本很大,你可以使用这个扩展:

public static String TrimBetween(this string text, int maxWhiteSpaces = 1)
{
    StringBuilder sb = new StringBuilder();
    int count = 0;
    foreach (Char c in text)
    {
        if (!Char.IsWhiteSpace(c))
        {
            count = 0;
            sb.Append(c);
        }
        else
        {
            if (++count <= maxWhiteSpaces)
                sb.Append(c);
        }
    }
    return sb.ToString();
}

然后很简单:

string text = "-----------------\r\n      Some text here      Some text here\r\n";
text = text.TrimBetween(2);

结果:

-----------------
Some text here  Some text here
于 2013-04-16T11:09:15.823 回答
-1

您可以使用 String.Replace 功能:

text=text.Replace("      ","  ");

但请记住,您应该在发布之前研究一下您的请求:这是非常基本的东西。

于 2013-04-16T10:46:10.023 回答