optimize the method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian

    optimize the method

    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;pl ant;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


  • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

    #2
    RE: optimize the method

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

    "Brian" wrote:
    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;pl ant;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
    >
    >
    >

    Comment

    • Mayur H Chauhan

      #3
      Re: optimize the method

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

      Mayur
      "Brian" <bsgallatin@com munity.nospamwr ote in message
      news:uSOgartBJH A.1180@TK2MSFTN GP04.phx.gbl...
      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;pl ant;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
      >
      >

      Comment

      • Brian

        #4
        Re: optimize the method

        what do you mean covert into stack?

        "Mayur H Chauhan" <mayur@orioninc .comwrote in message
        news:e0e%23XIvB JHA.4700@TK2MSF TNGP03.phx.gbl. ..
        Once you split string using ";" convet it into Stack and then you can
        search for any string. This would be faster too.
        >
        Mayur
        "Brian" <bsgallatin@com munity.nospamwr ote in message
        news:uSOgartBJH A.1180@TK2MSFTN GP04.phx.gbl...
        >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;p lant;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
        >>
        >>
        >
        >

        Comment

        • Mayur H Chauhan

          #5
          Re: optimize the method

          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).T oList()

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

          Else

          End If




          "Brian" <bsgallatin@com munity.nospamwr ote in message
          news:%23ZE90kvB JHA.3668@TK2MSF TNGP05.phx.gbl. ..
          what do you mean covert into stack?
          >
          "Mayur H Chauhan" <mayur@orioninc .comwrote in message
          news:e0e%23XIvB JHA.4700@TK2MSF TNGP03.phx.gbl. ..
          >Once you split string using ";" convet it into Stack and then you can
          >search for any string. This would be faster too.
          >>
          >Mayur
          >"Brian" <bsgallatin@com munity.nospamwr ote in message
          >news:uSOgartBJ HA.1180@TK2MSFT NGP04.phx.gbl.. .
          >>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
          >>>
          >>>
          >>
          >>
          >
          >

          Comment

          • =?ISO-8859-1?Q?G=F6ran_Andersson?=

            #6
            Re: optimize the method

            Brian wrote:
            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;pl ant;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.Esca pe("house")+"(; |$)").Success Then

            --
            Göran Andersson
            _____
            Göran Anderssons privata hemsida.

            Comment

            • Phill W.

              #7
              Re: optimize the method

              Brian wrote:
              I was wondering if there is a better way to do this....
              y="tree;bush;pl ant;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.

              Comment

              Working...