[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) ??
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)))
Re: string.contain(an element of array) ??
Did you try
if arrayname.contains(word)?
Re: string.contain(an element of array) ??
Quote:
Originally Posted by
dbasnett
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.
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?
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
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
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.
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
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:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim input = "Peter Piper picked a peck of pickled peppers."
Dim search = {"picked", "jar"}
Dim found = CheckViaLoop(input, search)
Dim found2 = CheckViaLinq(input, search)
search = {"pickles", "Joe"}
Dim found3 = CheckViaLoop(input, search)
Dim found4 = CheckViaLinq(input, search)
End Sub
Private Shared Function CheckViaLoop(ByVal input As String, ByVal search As String()) As Boolean
For Each item In search
If (input.Contains(item)) Then
Return True
End If
Next
Return False
End Function
Private Shared Function CheckViaLinq(ByVal input As String, ByVal search As String()) As Boolean
Return search.Any(Function(s) input.Contains(s))
End Function
Re: [RESOLVED] string.contain(an element of array) ??
thanks but more code than my original for each loop.
reg+ for
matp
.paul