如何从字符串中删除空格

本文关键字:删除 空格 字符串 | 更新日期: 2023-09-27 18:09:18

我有一个字符串值

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

是否有办法在每个有许多空格的地方删除2个空格?

谢谢你的帮助

如何从字符串中删除空格

可以使用正则表达式

var result = new Regex("'s{2,}").Replace(text,constForWhiteSpace)

这是你要找的吗?

text = text.Replace("   "," ");
Regex regex = new Regex(@"[ ]{2,}");    // Regex to match more than two occurences of space 
text = regex.Replace(text, @"");

试试....

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

输出:
SometexthereSometexthere

edit:这是为了速度,否则其他答案建议的正则表达式就可以了。这个函数非常快。

编辑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();
        }

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

text.Replace("       ", "     ");
var dirty = "asdjk    asldkjas dasdl l aksdjal;sd            asd;lkjdaslj";
var clean = string.Join(" ", dirty.Split(' ').Where(x => !string.IsNullOrEmpty(x)));

如果你不喜欢正则表达式或文本很大,你可以使用这个扩展:

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

试试这个:

var result = new Regex("'s{2,}").Replace(text,string.Empty)

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

//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...

您可以使用String。替换功能:

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

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