Repeated numbers

  • Thread starter Thread starter Robert Scheer
  • Start date Start date
R

Robert Scheer

Hi.

I am trying to write a boolean function that checks if the user
entered a sequence with 7 equal numbers, eg: 1111111, 2222222, 3333333
and so on. How can I do this in VB.NET ?

Thanks,
Robert Scheer
 
Robert Scheer said:
Hi.

I am trying to write a boolean function that checks if the user
entered a sequence with 7 equal numbers, eg: 1111111, 2222222,
3333333 and so on. How can I do this in VB.NET ?


Dim s As String = "1111111"
Dim b As Boolean

b = _
s.length = 7 _
andalso _
Array.TrueForAll(s.ToCharArray, Function(c As Char) (c = s(0)))

(VB 2008)


Armin
 
Another solution:

Dim a As String = "1111111"
Dim b As Boolean = False

b = a.Length = 7 AndAlso a.Replace(a.Substring(0, 1), "") = ""

Doesn't need VB 2008.

-Teemu
 
Teemu said:
Another solution:

Dim a As String = "1111111"
Dim b As Boolean = False

b = a.Length = 7 AndAlso a.Replace(a.Substring(0, 1), "") = ""

Doesn't need VB 2008.

-Teemu

But both of these codes evaluate to true for the string "AAAAAAA". I guess
the original poster should have some of the fun catching this case.
 
But both of these codes evaluate to true for the string "AAAAAAA". I
guess
the original poster should have some of the fun catching this case.

That's easy:

b = a.Length = 7 AndAlso IsNumeric(a) AndAlso a.Replace(a.Substring(0, 1),
"") = ""

-Teemu
 
Family Tree Mike said:
But both of these codes evaluate to true for the string "AAAAAAA".
I guess the original poster should have some of the fun catching
this case.


You're right.

b = _
s.Length = 7 _
AndAlso _
"0123456789".IndexOf(s(0)) > -1 _
AndAlso _
Array.TrueForAll(s.ToCharArray, Function(c As Char) (c = s(0)))


Better? ;-)


Armin
 
Armin Zingler said:
You're right.

b = _
s.Length = 7 _
AndAlso _
"0123456789".IndexOf(s(0)) > -1 _
AndAlso _
Array.TrueForAll(s.ToCharArray, Function(c As Char) (c = s(0)))


Better? ;-)


Armin

You both got it! I prefer the VB 2008 way (TrueForAll), but I may have gone
for an extension method to string.
 
You both got it! I prefer the VB 2008 way (TrueForAll), but I may have gone
for an extension method to string.

Sorry, the C# side of me slipped out...
 
Family Tree Mike said:
Sorry, the C# side of me slipped out...

Ok, let's extend this thread to extensions.... ;)


<System.Runtime.CompilerServices.Extension()> _
Public Function TrueForAll( _
ByVal s As String, ByVal match As Predicate(Of Char)) _
As Boolean
For Each c In s
If Not match(c) Then Return False
Next
Return True
End Function

<System.Runtime.CompilerServices.Extension()> _
Public Function EqualDigitsOnly(ByVal s As String) As Boolean

Return Char.IsDigit(s(0)) _
AndAlso _
s.TrueForAll(Function(c As Char) (c = s(0)))

End Function


Call:
b = s.Length = 7 AndAlso s.EqualDigitsOnly

;-)


Armin
 
Armin Zingler said:
Ok, let's extend this thread to extensions.... ;)


<System.Runtime.CompilerServices.Extension()> _
Public Function TrueForAll( _
ByVal s As String, ByVal match As Predicate(Of Char)) _
As Boolean
For Each c In s
If Not match(c) Then Return False
Next
Return True
End Function

<System.Runtime.CompilerServices.Extension()> _
Public Function EqualDigitsOnly(ByVal s As String) As Boolean

Return Char.IsDigit(s(0)) _
AndAlso _
s.TrueForAll(Function(c As Char) (c = s(0)))

End Function


Call:
b = s.Length = 7 AndAlso s.EqualDigitsOnly

;-)


Armin

Thanks! I didn't realize VB allowed for this!
 
what about this?

Private Function TrueForAll(ByVal s As String) As Boolean
Return IsNumeric(s) AndAlso Len(s) = 7 _
AndAlso Len(CStr(CSng(s)/ 1111111)) = 1
End Function

very simple of a simple mind
 
perhaps, if necessary, a litle correction if the number must be different
from zero

what about this?

Private Function TrueForAll(ByVal s As String) As Boolean
Return IsNumeric(s) AndAlso Len(s) = 7 _
AndAlso Len(CStr(CSng(s)/ 1111111)) = 1 andalso cint(s) > 0
End Function

very simple of a simple mind
 
Back
Top