Get leading whitespace

2019-05-24 07:05发布

I just wrote this method and I'm wondering if something similar already exists in the framework? It just seems like one of those methods...

If not, is there a better way to do it?

/// <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);
}

Thanks

Edit: after reading some comments, Its clear that I also need to handle tabs.

I can't give a very good example because the website trims spaces down to just one but I'll try:

Say the input is a string with 5 spaces, the method will return a string with 4 spaces. If the input is less than 4 spaces, it returns "". This might help:

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

5条回答
一夜七次
2楼-- · 2019-05-24 07:25

Nothing built in, but how about:

var result = line.TakeWhile(x => x == ' ');
if (trimToLowerTab)
    result = result.Skip(result.Count() % 4);
return new string(result.ToArray());
查看更多
该账号已被封号
3楼-- · 2019-05-24 07:26

I didn't run any performance tests but this is less code.

...

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

...
查看更多
孤傲高冷的网名
4楼-- · 2019-05-24 07:27

You should be using Char.IsWhiteSpace instead of comparing with ' ', usually. Not all "spaces" are ' '

查看更多
小情绪 Triste *
5楼-- · 2019-05-24 07:35

What about an extension method on String? I passed in the tabLength to make the function more flexible. I also added a separate method to return the whitespace length since one comment that that is what you were looking for.

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;
}
查看更多
趁早两清
6楼-- · 2019-05-24 07:39

I'm sure there's nothing built in, but you can use a regular expression to do this if you're comfortable with them. This matches any whitespace at the beginning of the line:

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

NOTE: This would not perform as well as a simple loop. I would just go with your implementation.

查看更多
登录 后发表回答