By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,670 Members | 1,554 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,670 IT Pros & Developers. It's quick & easy.

Check whether a string is inside a number of strings

P: n/a
Jan
Hi,

Is there any elegant way in VB.NET to check whether a certain string is part
of a "collection" of strings.

Something like IF "teststring" in {"string1", "string2", "string3",
"teststring"} THEN

Thanks,
Jan
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
have a look at the arraylist class
that has a search and boolean search
copy your array to an arraylist, (sort it and do a boolean search), search
it

eric

"Jan" <ms****@tss.be> wrote in message
news:OG**************@TK2MSFTNGP11.phx.gbl...
Hi,

Is there any elegant way in VB.NET to check whether a certain string is part of a "collection" of strings.

Something like IF "teststring" in {"string1", "string2", "string3",
"teststring"} THEN

Thanks,
Jan

Nov 20 '05 #2

P: n/a
Cor
Hi Jan,

I did wish there was an indexofany(string())

That is as far as I know not, but maybe you can do it with something like
this,
I did made some more in this sample than you asked, so it can shorter.

\\\\
Dim mytestvalues() As String = {"Jan", "funny", "string3"}
For i As Integer = 0 To mytestvalues.Length
Dim y As Integer = _
"This is a FunnyString Approach".ToLower.IndexOf(mytestvalues(i))
If y <> -1 Then
MessageBox.Show("It is " & mytestvalues(i) & _
" on postition " & y.ToString)
Exit For
End If
Next
///
That string is normaly of course
dim x as string = "This is a FunnyString Approach" and then
y = x.ToLower.IndexOf(mytestvalues(i))

I hope this helps a little bit?

Cor
Is there any elegant way in VB.NET to check whether a certain string is part of a "collection" of strings.

Something like IF "teststring" in {"string1", "string2", "string3",
"teststring"} THEN

Thanks,
Jan

Nov 20 '05 #3

P: n/a
Jan
Cor,

Looks like IndexOf is a step in the right direction :

IF ",string1,string2,string3,teststring,".indexof ("," & "teststring" & ",")
<> 1 THEN ....

- should work provided teststring does not contain any ","
- is short
- but is not so elegant ....

Anything more elegant ?

Thanks,
Jan
"Cor" <no*@non.com> wrote in message
news:eK****************@TK2MSFTNGP12.phx.gbl...
Hi Jan,

I did wish there was an indexofany(string())

That is as far as I know not, but maybe you can do it with something like
this,
I did made some more in this sample than you asked, so it can shorter.

\\\\
Dim mytestvalues() As String = {"Jan", "funny", "string3"}
For i As Integer = 0 To mytestvalues.Length
Dim y As Integer = _
"This is a FunnyString Approach".ToLower.IndexOf(mytestvalues(i))
If y <> -1 Then
MessageBox.Show("It is " & mytestvalues(i) & _
" on postition " & y.ToString)
Exit For
End If
Next
///
That string is normaly of course
dim x as string = "This is a FunnyString Approach" and then
y = x.ToLower.IndexOf(mytestvalues(i))

I hope this helps a little bit?

Cor
Is there any elegant way in VB.NET to check whether a certain string is

part
of a "collection" of strings.

Something like IF "teststring" in {"string1", "string2", "string3",
"teststring"} THEN

Thanks,
Jan


Nov 20 '05 #4

P: n/a
* "Jan" <ms****@tss.be> scripsit:
Is there any elegant way in VB.NET to check whether a certain string is part
of a "collection" of strings.

Something like IF "teststring" in {"string1", "string2", "string3",
"teststring"} THEN


An elegant way is IMO a loop ('For...To') in which you compare the
strings to the string. Notice that this may have bad performance if
there are many strings. Using, for example, binary search in a sorted
list of strings or special tree structures may be faster.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #5

P: n/a
Cor
Hi Jan,

Looks if I was looking in the wrong direction.

This one exist but I neve tried it so that one you have to try yourself

array.indexof

http://msdn.microsoft.com/library/de...exOfTopic1.asp

This is just one of the overload methods there are plenty.

I hope this helps?

Cor
Nov 20 '05 #6

P: n/a
Jan
Cor,

Perfect !

So it becomes :

dim asPossibleValues() as string = {"test1", "test2", "test3"}
if Array.IndexOf(asPossibleValues,"test3") <> -1 then
.......

- works well
- concise
- elegant

Thanks a lot,
Jan
"Cor" <no*@non.com> wrote in message
news:uK**************@TK2MSFTNGP10.phx.gbl...
Hi Jan,

Looks if I was looking in the wrong direction.

This one exist but I neve tried it so that one you have to try yourself

array.indexof

http://msdn.microsoft.com/library/de...exOfTopic1.asp
This is just one of the overload methods there are plenty.

I hope this helps?

Cor

Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.