计算字符串开头的空格数
Count the number of whitespaces at the beginng of the string
有没有使用 vb.net 来计算字符串开头的空格的简单方法?
例如我的字符串是“这是一个测试”。开头的空格数是3个。有没有内置函数可以编程计算?
Trim 领先的白色 space 并比较长度:
num = s.Length - LTrim(s).Length
或找到第一个非白色space字符并检查其索引:
Dim match = System.Text.RegularExpressions.Regex.Match(s, "\S")
If match.Success Then
num = match.Index
End If
我发现这在 VB.Net 中效果很好,并且优点是它很短,不会干扰您的方法流程。尽管使用循环进行计数,但此 C# post suggest that a Loop is surprisingly efficient. My application does not warrant doing the timing tests for this use of Linq functionality in VB.Net - perhaps someone else's does and they could comment here? Credits to @Henk Holtermann.
中提供的计时测试
Dim WhiteSpaceCount As Integer 'The number of white spaces at the start
WhiteSpaceCount = MyString.TakeWhile(Function(C As Char) Char.IsWhiteSpace(C)).Count
有没有使用 vb.net 来计算字符串开头的空格的简单方法?
例如我的字符串是“这是一个测试”。开头的空格数是3个。有没有内置函数可以编程计算?
Trim 领先的白色 space 并比较长度:
num = s.Length - LTrim(s).Length
或找到第一个非白色space字符并检查其索引:
Dim match = System.Text.RegularExpressions.Regex.Match(s, "\S")
If match.Success Then
num = match.Index
End If
我发现这在 VB.Net 中效果很好,并且优点是它很短,不会干扰您的方法流程。尽管使用循环进行计数,但此 C# post suggest that a Loop is surprisingly efficient. My application does not warrant doing the timing tests for this use of Linq functionality in VB.Net - perhaps someone else's does and they could comment here? Credits to @Henk Holtermann.
中提供的计时测试Dim WhiteSpaceCount As Integer 'The number of white spaces at the start
WhiteSpaceCount = MyString.TakeWhile(Function(C As Char) Char.IsWhiteSpace(C)).Count