Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,884 questions

51,810 answers

573 users

How to use Array.Exists<T> method to check whether any string in string array begin with a specified character in VB.NET

2 Answers

0 votes
Module Module1

    Sub Main()

        Dim s() As String = {"PHP", "C", "C++", "C#", "Java", "JavaScript",
                             "VB.NET", "VB6", "Pascal", "Python"}

        Dim characters() As Char = {"C"c, "J"c, "V"c, "P"c, "D"c}

        For Each ch In characters
            Console.WriteLine("One or more strings begin with '{0}': {1}",
                              ch,
                              Array.Exists(s, AddressOf (New SearchString(ch)).StartsWith))
        Next

    End Sub

    Public Class SearchString
        Dim firstChar As Char

        Public Sub New(firstChar As Char)
            Me.firstChar = Char.ToUpper(firstChar)
        End Sub

        Public Function StartsWith(s As String) As Boolean
            If String.IsNullOrEmpty(s) Then Return False

            If s.Substring(0, 1).ToUpper = firstChar Then
                Return True
            Else
                Return False
            End If
        End Function
    End Class

End Module

' run:
' 
' One or more strings begin with 'C': True
' One Or more strings begin with 'J': True
' One Or more strings begin with 'V': True
' One Or more strings begin with 'P': True
' One Or more strings begin with 'D': False




answered Apr 16, 2016 by avibootz
0 votes
Module Module1

    Sub Main()

        Dim str() As String = {"PHP", "C", "C++", "C#", "Java", "JavaScript",
                               "VB.NET", "VB6", "Pascal", "Python"}

        Dim characters() As Char = {"C"c, "J"c, "V"c, "P"c, "D"c}

        For Each ch In characters
            Console.WriteLine("One or more strings begin with '{0}': {1}",
                           ch,
                           Array.Exists(str, Function(s)
                                                 If String.IsNullOrEmpty(s) Then Return False

                                                 If s.Substring(0, 1).ToUpper = ch Then
                                                     Return True
                                                 Else
                                                     Return False
                                                 End If
                                             End Function))
        Next

    End Sub

End Module

' run:
' 
' One or more strings begin with 'C': True
' One Or more strings begin with 'J': True
' One Or more strings begin with 'V': True
' One Or more strings begin with 'P': True
' One Or more strings begin with 'D': False

 



answered Apr 16, 2016 by avibootz
...