Results 1 to 9 of 9

Thread: [RESOLVED] string.contain(an element of array) ??

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Posts
    22

    Resolved [RESOLVED] string.contain(an element of array) ??

    i have a long string taken from inputbox and an array/arraylist/hashset(of string) taken from file.readalllines(path)

    i know can do:

    function
    for each word in array
    if string.contain word
    return true


    but there is a string.contain(an element of array) ??

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: string.contain(an element of array) ??

    try this:

    Code:
    Dim s As String = "abcdefghij"
    Dim a() As String = {"e", "a", "s", "y"}
    MsgBox(a.Any(Function(str) s.Contains(str)))

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: string.contain(an element of array) ??

    Did you try

    if arrayname.contains(word)?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Posts
    22

    Re: string.contain(an element of array) ??

    Quote Originally Posted by dbasnett View Post
    Did you try

    if arrayname.contains(word)?
    string is "this is example string i will use"
    array: [example, red, use]

    so cannot use what you suggest


    Quote Originally Posted by .paul. View Post
    try this:

    Code:
    Dim s As String = "abcdefghij"
    Dim a() As String = {"e", "a", "s", "y"}
    MsgBox(a.Any(Function(str) s.Contains(str)))
    work great! thanks!!
    can you explain?
    any is methid i was looking for, function(str)? trasform s in array of string? and later check if contain?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: [RESOLVED] string.contain(an element of array) ??

    the first line is the string to test
    the second line is the array to test
    the third line says return true if any element of the array exists in the string

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Posts
    22

    Re: [RESOLVED] string.contain(an element of array) ??

    line 1 & 2 was clear, was 3 that i understand half ahaha

    anyway is same of for each word in array string.contain(word) only in 1 line lol

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Posts
    22

    Re: [RESOLVED] string.contain(an element of array) ??

    line 1 & 2 was clear, was 3 that i understand half ahaha

    anyway is same of for each word in array string.contain(word) only in 1 line lol

    Quote Originally Posted by .paul. View Post
    the first line is the string to test
    the second line is the array to test
    the third line says return true if any element of the array exists in the string
    somethigs faster?
    if array have alot of items... is slow

  8. #8
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: [RESOLVED] string.contain(an element of array) ??

    You may be able to eek out some performance by using a For Each loop over Linq. You need to make sure that you short circuit to exit out of the loop when you make a match though.

    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.         Dim input = "Peter Piper picked a peck of pickled peppers."
    3.         Dim search = {"picked", "jar"}
    4.  
    5.         Dim found = CheckViaLoop(input, search)
    6.  
    7.         Dim found2 = CheckViaLinq(input, search)
    8.  
    9.         search = {"pickles", "Joe"}
    10.  
    11.         Dim found3 = CheckViaLoop(input, search)
    12.  
    13.         Dim found4 = CheckViaLinq(input, search)
    14.     End Sub
    15.  
    16.     Private Shared Function CheckViaLoop(ByVal input As String, ByVal search As String()) As Boolean
    17.         For Each item In search
    18.             If (input.Contains(item)) Then
    19.                 Return True
    20.             End If
    21.         Next
    22.         Return False
    23.     End Function
    24.  
    25.     Private Shared Function CheckViaLinq(ByVal input As String, ByVal search As String()) As Boolean
    26.         Return search.Any(Function(s) input.Contains(s))
    27.     End Function
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Posts
    22

    Re: [RESOLVED] string.contain(an element of array) ??

    thanks but more code than my original for each loop.
    reg+ for
    matp
    .paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width