string question

  • Thread starter Thread starter microsoft.news.com
  • Start date Start date
M

microsoft.news.com

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

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


Jared Parsons said:
Checkout String.Split()

--
Jared Parsons [MSFT]
(e-mail address removed)
http://blogs.msdn.com/jaredpar
"This posting is provided "AS IS" with no warranties, and confers no
rights"
microsoft.news.com said:
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

?
 
microsoft.news.com said:
won't i have to loop through it to as well?
I'm new to this C# stuff

Yes, for example:

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

will display as:

BMW
Chevy
BMW
Lexus

/LM
 
Be aware, however, that

cars.Split(' ')

and

cars.Split()

act differently. You probably want the latter.
 
For another option, how about using RegEx?

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

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