获取前导空白
我刚刚写了这个方法,我想知道框架中是否已经存在类似的东西?这似乎只是其中一种方法...
如果没有,是否有更好的方法?
/// <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);
}
谢谢
编辑: 阅读一些评论后,很明显我还需要处理选项卡。
我无法给出一个很好的例子,因为该网站将空格削减为 1 个,但我会尝试:
假设输入是一个包含 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
...
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
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我没有运行任何性能测试,但这代码较少。
I didn't run any performance tests but this is less code.
对于其他希望将空格作为字符串获取的人来说,我个人认为这很简单明了:
只需将所有不是前导空格替换为空字符串即可。这也适用于任何类型的空白 - 不仅仅是空格。感谢 @bobwki 指出了失败的边缘情况 - 我添加了对空字符串的检查。
For anyone else looking to get the whitespace as a string, I personally found this to be simple and straightforward:
Just replace everything that's not the leading whitespace with an empty string. This will also work for any sort of whitespace - not just spaces. Thanks to @bobwki for pointing out an edge case where this failed - I've added a check for empty strings.
您应该使用 Char.IsWhiteSpace 而不是与通常是
' '
。并非所有“空格”都是' '
You should be using Char.IsWhiteSpace instead of comparing with
' '
, usually. Not all "spaces" are' '
我确信没有内置任何内容,但如果您熟悉正则表达式,则可以使用正则表达式来执行此操作。这会匹配行开头的任何空格:
注意:这不会像简单循环那样执行。我会同意你的实施。
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:
NOTE: This would not perform as well as a simple loop. I would just go with your implementation.
String 上的扩展方法怎么样?我传入了tabLength以使功能更加灵活。我还添加了一个单独的方法来返回空白长度,因为有评论说这就是您正在寻找的内容。
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.
没有内置任何东西,但是怎么样:
Nothing built in, but how about:
以柯克的答案为基础
Building on Kirk's answer