string question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • microsoft.news.com

    string question

    I have a string like this:

    BMW Chevy BMW Lexus

    How can I read this string and get each value 1 at a time like

    BMW
    Chevy
    BMW
    Lexus

    ?


  • Jared Parsons [MSFT]

    #2
    Re: string question

    Checkout String.Split()

    --
    Jared Parsons [MSFT]
    jaredpar@online .microsoft.com

    "This posting is provided "AS IS" with no warranties, and confers no rights"
    "microsoft.news .com" <CSharpCoder> wrote in message
    news:%23bH1LhOU FHA.3176@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    >I have a string like this:
    >
    > BMW Chevy BMW Lexus
    >
    > How can I read this string and get each value 1 at a time like
    >
    > BMW
    > Chevy
    > BMW
    > Lexus
    >
    > ?
    >
    >[/color]


    Comment

    • microsoft.news.com

      #3
      Re: string question

      won't i have to loop through it to as well?
      I'm new to this C# stuff


      "Jared Parsons [MSFT]" <jaredpar@onlin e.microsoft.com > wrote in message
      news:u5cn3pOUFH A.4056@TK2MSFTN GP15.phx.gbl...[color=blue]
      > Checkout String.Split()
      >
      > --
      > Jared Parsons [MSFT]
      > jaredpar@online .microsoft.com
      > http://blogs.msdn.com/jaredpar
      > "This posting is provided "AS IS" with no warranties, and confers no
      > rights"
      > "microsoft.news .com" <CSharpCoder> wrote in message
      > news:%23bH1LhOU FHA.3176@TK2MSF TNGP12.phx.gbl. ..[color=green]
      >>I have a string like this:
      >>
      >> BMW Chevy BMW Lexus
      >>
      >> How can I read this string and get each value 1 at a time like
      >>
      >> BMW
      >> Chevy
      >> BMW
      >> Lexus
      >>
      >> ?
      >>
      >>[/color]
      >
      >[/color]


      Comment

      • Lebesgue

        #4
        Re: string question

        > won't i have to loop through it to as well?[color=blue]
        > I'm new to this C# stuff[/color]
        Yes, you would have to, but I suggest you trying more than theoretizing.


        Comment

        • Luc E. Mistiaen

          #5
          Re: string question


          "microsoft.news .com" <me@me.com> wrote in message
          news:%23ptEZGPU FHA.2892@TK2MSF TNGP14.phx.gbl. ..[color=blue]
          > won't i have to loop through it to as well?
          > I'm new to this C# stuff
          >[/color]

          Yes, for example:

          string cars = "BMW Chevy BMW Lexus" ;
          foreach (car in cars.Split (' ')) Console.WriteLi ne (car) ;

          will display as:

          BMW
          Chevy
          BMW
          Lexus

          /LM


          Comment

          • Bruce Wood

            #6
            Re: string question

            Be aware, however, that

            cars.Split(' ')

            and

            cars.Split()

            act differently. You probably want the latter.

            Comment

            • joelcochran@gmail.com

              #7
              Re: string question

              For another option, how about using RegEx?

              <code>
              string pattern = @"\s?.*?\s" ; // Whatever the real pattern is...
              MatchCollection matches = Regex.Matches( yourString , pattern ,
              RegexOptions.Ex plicitCapture );

              foreach ( Match m in matches )
              {
              string theFoundText = m.Value ;
              }
              </code>

              I'm using this in my SQL Syntax Highlighter and it is very very fast.

              Joel

              Comment

              Working...