optimize the method

B

Brian

Hi,
I was wondering if there is a better way to do this....
This is my example....
Dim x as String()
dim y as string
dim i as string

y="tree;bush;plant;grass"
x = y.split(cchar(";"),y)
i= "house"
if i = x().? then
'do something
end if
Basically is there a way in dotnet to look into the arraylist for i without
looping through each y?

Thanks,
Brian
 
F

Family Tree Mike

You can use the fact that y will start with "house;", end with ";house", or
contain ";house;". Test that with StartsWith, EndsWith, or IndexOf.
 
M

Mayur H Chauhan

Once you split string using ";" convet it into Stack and then you can search
for any string. This would be faster too.

Mayur
 
B

Brian

what do you mean covert into stack?

Mayur H Chauhan said:
Once you split string using ";" convet it into Stack and then you can
search for any string. This would be faster too.

Mayur
 
M

Mayur H Chauhan

I am sorry for my mistake. Instead of Stack, you can have List. Here is an
example

Dim s As String = "1;2"

Dim r = s.Split(";"c).ToList()

If r.Contains("1") = True Then

Else

End If
 
G

Göran Andersson

Brian said:
Hi,
I was wondering if there is a better way to do this....
This is my example....
Dim x as String()
dim y as string
dim i as string

y="tree;bush;plant;grass"
x = y.split(cchar(";"),y)
i= "house"
if i = x().? then
'do something
end if
Basically is there a way in dotnet to look into the arraylist for i without
looping through each y?

Thanks,
Brian

Once you have split the data into an array, there is no way of searching
for a string in the array without looping. There are several different
ways that you can search the array, but there is no method that can do
it without looping the array.

As Mike suggested, you should search the string without splitting it.

You can use a regular expression:

If RegEx.Match("(^|;)"+RegEx.Escape("house")+"(;|$)").Success Then
 
P

Phill W.

Brian said:
I was wondering if there is a better way to do this....
y="tree;bush;plant;grass"
x = y.split(cchar(";"),y)
i= "house"
if i = x().? then
'do something
end if
Basically is there a way in dotnet to look into the arraylist for i without
looping through each y?

If you really mean "look into the /ArrayList/", then yes - ArrayList has
a Contains method:

x = New ArrayList
x.Add( tree" )
.. . .
x.Add( grass" )

Dim i as String = "house"
If x.Contains( i ) Then
. . .
End If

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top