2

I'm wondering if there's a better way of writing these multiple If statements? I'm sure there is, I just can't figure out what it'd be. In essence the code is just shortening the string.

                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

Any help is much appreciated.

  • You could use regex; identifying an arbitrary number of repetitions of a single character is very straightforward as I recall. If performance is a concern, I'd recommend profiling; as ugly as the code you've posted is, it's probably faster than any of the alternatives. – Craig Jul 28 '17 at 13:39

3 Answers3

5

You could use LINQ:

If text.Length > 0 AndAlso text.All(Function(c) c = "-"c) Then text = "-"

Requested explanation (i found this actually pretty understandable):

Since a string implements IEnumerable(Of Char) you can use it like a collection of characters. The LINQ extension method Enumerable.All determines if all items in a sequence/collection match a given predicate(returns True). In this case the predicate checks whether a given char is "-"c(the c at the end is necessary with option strict on to tell the compiler that this is a char and not a string). So only if all chars in the string are minuses this method returns True. As soon as All finds a different char it will return False.

If it returns True there are 1-n minuses and no other char, so the variable text can be "-".

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

what about?:

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")

, but if do not need limitation, then remove

xxx.Length <= maxLengthOfStringYouCompareTo
0
While text.Contains("--")
    text = text.Replace("--","-")
End While
muffi
  • 366
  • 6
  • 18
  • I've been downvoted before with this comment: I believe you are being down voted because, while code snippets are fine, answers aren't really worth much without some attempt at an explanation of what the code snippet is doing. – Mike_OBrien Jun 13 at 16:11 And Mike's comment has been upvoted 4 times. With the same rationale, the two answers above shall be downvoted too. – Alessandro Mandelli Jul 28 '17 at 11:17
  • It's quite obvious what the code snippet is doing though... is it not? Seems a bit pedantic. – Codey McCodeface Jul 28 '17 at 11:19
  • https://image.prntscr.com/image/dwPxSi8eTsKFEh0KSP01VQ.png Judge yourself. – Alessandro Mandelli Jul 28 '17 at 12:21
  • Downvoted because while code snippets are fine, answers aren't really worth much without some attempt at an explanation of what the code snippet is doing – Alessandro Mandelli Jul 28 '17 at 14:08
  • @AlessandroMandelli: the reason why you got downvotes [here](https://stackoverflow.com/a/44523866/284240) was not (only) that it was a code only answer. But because it didn't help OP to solve his issue(find checked checkboxes). it was just the first step. Another problem with your and the accepted answers approach: you don't find the checkboxes because they are nested in child controls. – Tim Schmelter Jul 28 '17 at 14:29
  • Users like Alessandro Mandelli force me not to be more active here than now. – muffi Aug 01 '17 at 12:25