vb.net - Multiple if statements checking length of string and shortening -
i'm wondering if there's better way of writing these multiple if statements? i'm sure there is, can't figure out it'd be. in essence code shortening string.
if text = "-----------------" text = "-" end if if text = "----------------" text = "-" end if if text = "---------------" text = "-" end if if text = "--------------" text = "-" end if if text = "-------------" text = "-" end if if text = "------------" text = "-" end if if text = "-----------" text = "-" end if if text = "----------" text = "-" end if if text = "---------" text = "-" end if if text = "--------" text = "-" end if if text = "-------" text = "-" end if if text = "------" text = "-" end if if text = "-----" text = "-" end if if text = "----" text = "-" end if if text = "---" text = "-" end if if text = "--" text = "-" end if
any appreciated.
you use linq:
if text.length > 0 andalso text.all(function(c) c = "-"c) text = "-"
requested explanation (i found pretty understandable):
since string implements ienumerable(of char)
can use collection of characters. linq extension method enumerable.all
determines if items in sequence/collection match given predicate(returns true
). in case predicate checks whether given char "-"c
(the c @ end necessary option strict on
tell compiler char , not string). if chars in string minuses method returns true
. all
finds different char return false
.
if returns true
there 1-n minuses , no other char, variable text
can "-"
.
Comments
Post a Comment