获得领先的空白

问题描述:

我刚刚写了这个方法,我想知道如果类似的东西已经存在于框架中?它只是看起来像这些方法之一...获得领先的空白

如果不是,有没有更好的方法来做到这一点?

/// <summary> 
/// Return the whitespace at the start of a line. 
/// </summary> 
/// <param name="trimToLowerTab">Round the number of spaces down to the nearest multiple of 4.</param> 
public string GetLeadingWhitespace(string line, bool trimToLowerTab = true) 
{ 
    int whitespace = 0; 
    foreach (char ch in line) 
    { 
     if (ch != ' ') break; 
     ++whitespace; 
    } 

    if (trimToLowerTab) 
     whitespace -= whitespace % 4; 

    return "".PadLeft(whitespace); 
} 

感谢

编辑: 读了一些评论,它清楚,我还需要处理的标签后。

,因为网站空间修剪下降到只有一个,但我会努力,我不能给出一个很好的例子:

说出输入与5位的字符串,该方法将返回与4串空间。如果输入少于4个空格,则返回""。 这可能有所帮助:

input spaces | output spaces 
0 | 0 
1 | 0 
2 | 0 
3 | 0 
4 | 4 
5 | 4 
6 | 4 
7 | 4 
8 | 8 
9 | 8 
... 
+0

你可以给一些样本输入/输出吗?目前还不完全清楚你想从你的代码中做什么。例如,如果第一个字符不是空白和'trimToLowerTab == false',则'whitespace == 0'。因此,无论线的长度如何,总是以“return”“.PadLeft(0)”结尾。如果第二个字符不是空格,则总是以1个空格结束,等等。我没有看到这些情况下舍入的位置。多一点上下文也会有帮助。 – 2010-10-11 15:59:54

+0

所以如果我给字符串''e“'(想象3个空格),那么方法返回的字符串应该是”“,因为只有3个空格。但是,如果输入字符串是“e”(5个空格),则返回的字符串将是'“”'(4个空格)(在空间总数之下的最接近4的倍数)。如果参数是错误的,那么前导空格只是在没有任何修改的情况下给出。编辑:网站从评论中删除空白... – Nobody 2010-10-11 16:03:17

+0

你能编辑你的问题,并把你的例子作为代码吗?我在您的评论中看不到超过一个连续的空间。 – 2010-10-11 16:05:48

对String的扩展方法怎么样?我通过tabLength使函数更加灵活。我还添加了一个单独的方法来返回空白长度,因为一个评论是你正在寻找的。

public static string GetLeadingWhitespace(this string s, int tabLength = 4, bool trimToLowerTab = true) 
{ 
    return new string(' ', s.GetLeadingWhitespaceLength()); 
} 

public static int GetLeadingWhitespaceLength(this string s, int tabLength = 4, bool trimToLowerTab = true) 
{ 
    if (s.Length < tabLength) return 0; 

    int whiteSpaceCount = 0; 

    while (Char.IsWhiteSpace(s[whiteSpaceCount])) whiteSpaceCount++; 

    if (whiteSpaceCount < tabLength) return 0; 

    if (trimToLowerTab) 
    { 
    whiteSpaceCount -= whiteSpaceCount % tabLength; 
    } 

    return whiteSpaceCount; 
} 
+0

非常好,谢谢。我无法相信,我没有想到它是一种扩展方法! – Nobody 2010-10-11 18:07:10

+0

如果您在具有5个空格的字符串上调用GetLeadingWhitespaceLength(),该代码是否会引发异常? – 2014-01-31 16:14:56

+0

我现在没有时间调试它,但我不认为它会抛出。如果您通过SSSSSX(其中S ==空格),那么第一个“if”不会被传递,因此不会返回零。 while循环简单地计数前导空格,在这种情况下,whiteSpaceCount = 5。whiteSpaceCount(5)不小于tabLength(4),因此不返回零。 trimToLowerTab为true,所以whiteSpaceCount减少了whiteSpaceCount(5)%tabLength(4)(1)。因此,whiteSpaceCount最终是4. – wageoghe 2014-01-31 18:42:41

我没有运行任何性能测试,但是这是更少的代码。

... 

whitespace = line.Length - line.TrimStart(' ').Length; 

... 
+2

注意:您可以删除''''来获取所有空格而不是''''。 – 2010-10-11 16:05:10

+0

感谢这就是我正在寻找的 – Nobody 2010-10-11 16:06:08

+0

@rmx - 那么我先前的评论是没有意义的。 :)奥斯汀的答案返回一个整数。如果你想把它转换成实际的空白,你可以'返回新的字符串'('',whitespace);' – 2010-10-11 16:10:44

没有建成,但如何:

var result = line.TakeWhile(x => x == ' '); 
if (trimToLowerTab) 
    result = result.Skip(result.Count() % 4); 
return new string(result.ToArray()); 

您应该使用Char.IsWhiteSpace,而不是与' '比较,通常。并非所有的“空格”都是' '

我确定没有内置任何内容,但是如果您对它们感到满意,可以使用正则表达式来执行此操作。这匹配任何行开头的空格:

public static string GetLeadingWhitespace(string line) 
{ 
    return Regex.Match(line, @"^([\s]+)").Groups[1].Value; 
} 

注意:这不会像简单循环那样执行。我只想跟你的实施一起去。

+0

另外:我忽略了“最接近4个部分的倍数”,原来的问题声称它并不重要。 – steinar 2010-10-11 16:25:11