optimize the method

  • Thread starter Thread starter Brian
  • Start date Start date
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
 
You can use the fact that y will start with "house;", end with ";house", or
contain ";house;". Test that with StartsWith, EndsWith, or IndexOf.
 
Once you split string using ";" convet it into Stack and then you can search
for any string. This would be faster too.

Mayur
 
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
 
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
 
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
 
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.
 
Back
Top