|
-
May 10th, 2013, 06:07 AM
#1
Thread Starter
Junior Member
[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) ??
-
May 10th, 2013, 06:12 AM
#2
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)))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 10th, 2013, 06:13 AM
#3
Re: string.contain(an element of array) ??
Did you try
if arrayname.contains(word)?
-
May 10th, 2013, 07:45 AM
#4
Thread Starter
Junior Member
Re: string.contain(an element of array) ??
 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
 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?
-
May 10th, 2013, 07:51 AM
#5
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 10th, 2013, 08:01 AM
#6
Thread Starter
Junior Member
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
-
May 10th, 2013, 08:03 AM
#7
Thread Starter
Junior Member
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
 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
-
May 10th, 2013, 09:31 AM
#8
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
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.
-
May 10th, 2013, 11:26 AM
#9
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|