Vb.net 检查字符串长度和缩短的多个if语句

Vb.net 检查字符串长度和缩短的多个if语句,vb.net,performance,if-statement,Vb.net,Performance,If Statement,我想知道是否有更好的方法来编写这些多if语句?我肯定有,我只是不知道会是什么。本质上,代码只是缩短字符串 If text = "-----------------" Then text = "-" End If If text = "----------------" Then text = "-"

我想知道是否有更好的方法来编写这些多if语句?我肯定有,我只是不知道会是什么。本质上,代码只是缩短字符串

                If text = "-----------------" Then
                    text = "-"
                End If
                If text = "----------------" Then
                    text = "-"
                End If
                If text = "---------------" Then
                    text = "-"
                End If
                If text = "--------------" Then
                    text = "-"
                End If
                If text = "-------------" Then
                    text = "-"
                End If
                If text = "------------" Then
                    text = "-"
                End If
                If text = "-----------" Then
                    text = "-"
                End If
                If text = "----------" Then
                    text = "-"
                End If
                If text = "---------" Then
                    text = "-"
                End If
                If text = "--------" Then
                    text = "-"
                End If
                If text = "-------" Then
                    text = "-"
                End If
                If text = "------" Then
                    text = "-"
                End If
                If text = "-----" Then
                    text = "-"
                End If
                If text = "----" Then
                    text = "-"
                End If
                If text = "---" Then
                    text = "-"
                End If
                If text = "--" Then
                    text = "-"
                End If
非常感谢您的帮助。

您可以使用LINQ:

If text.Length > 0 AndAlso text.All(Function(c) c = "-"c) Then text = "-"
要求解释(我发现这其实很容易理解):

因为字符串实现了
IEnumerable(字符)
,所以可以像使用字符集合一样使用它。LINQ扩展方法
可枚举。All
确定序列/集合中的所有项是否与给定项匹配(返回
True
)。在这种情况下,谓词检查给定的字符是否为
“-”c
(末尾的c必须带有
选项strict on
,以告诉编译器这是字符而不是字符串)。因此,只有当字符串中的所有字符都是负数时,此方法才会返回
True
。只要
All
找到不同的字符,它就会返回
False

如果它返回
True
则有1-n个减号,并且没有其他字符,因此变量
text
可以是
“-”

怎么样

Dim maxLengthOfStringYouCompareTo As Integer = 17

Dim xxx As String = ""

Dim text As String = If((xxx.All(Function(charrr) charrr.ToString() = "-") OrElse xxx.Length <= maxLengthOfStringYouCompareTo), "-", "otherValue")
Dim maxLengthOfStringYouCompareTo为整数=17
Dim xxx As String=“”

将文本尺寸标注为字符串=If((xxx.All(函数(charrr)charrr.ToString()=“-”)OrElse xxx.Length我之前曾被否决过这一评论:我相信你被否决了,因为虽然代码片段很好,但如果不尝试解释代码片段的作用,答案就没有多大价值。-Mike_OBrien Jun 13 16:11,Mike的评论被否决了4次。同样的道理,t上面的两个答案也应该被否决。很明显,代码段在做什么……不是吗?似乎有点迂腐。判断一下你自己。否决是因为虽然代码段很好,但如果不尝试解释代码段是什么,答案就没有多大价值doing@AlessandroMandelli当前位置你被解雇的原因否决票并不仅仅是因为它是一个代码唯一的答案,而是因为它无助于OP解决他的问题(找到复选框)。这只是第一步。您和接受答案方法的另一个问题是:您找不到复选框,因为它们嵌套在子控件中。您可以使用正则表达式;正如我所记得的,识别单个字符的任意重复次数非常简单。如果性能是一个问题,我建议进行分析;如尽管您发布的代码很简单,但它可能比其他任何代码都快。否决票是因为虽然代码片段很好,但如果不尝试解释代码片段是什么,答案就没有多大价值doing@AlessandroMandelli:固定
xxx.Length <= maxLengthOfStringYouCompareTo
While text.Contains("--")
    text = text.Replace("--","-")
End While