我想知道是否有更好的方式来编写这些多个If语句?我确定有,我不知道它会是什么。本质上,代码只是缩短了字符串。多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
任何帮助,非常感谢。既然字符串实现IEnumerable(Of Char)
你可以使用它像字符的集合
:
你可以使用正则表达式;我记得,识别单个字符的任意数量的重复是非常简单的。如果性能是一个问题,我建议分析;就像你发布的代码一样丑陋,它可能比任何其他代码都快。 – Craig