Split

S

shapper

Hello,

I have a string as follows:

string text = "Paris, New York , London,Lisbon,Oslo, Chicago ";

I need to get all words with no commas or spaces:
"Paris", "New", "York", "London", "Lisbon", "Oslo", "Chicago"

I am using (I think it is ok):
string[] words = text.Split(new char[] { ',' , ' '})

I need to get 3 words of the all I got. It can be the first 3 or any
other 3. It does no matter.

So words would have, for example: "Paris", "New", "York"

Is there a way to do this without using a loop?

Thanks,
Miguel
 
E

Eric B.

shapper said:
Hello,

I have a string as follows:

string text = "Paris, New York , London,Lisbon,Oslo, Chicago ";

I need to get all words with no commas or spaces:
"Paris", "New", "York", "London", "Lisbon", "Oslo", "Chicago"

I am using (I think it is ok):
string[] words = text.Split(new char[] { ',' , ' '})

I need to get 3 words of the all I got. It can be the first 3 or any
other 3. It does no matter.

So words would have, for example: "Paris", "New", "York"

Is there a way to do this without using a loop?

Thanks,
Miguel


text.Split to get the words then text.Trim() to remove the trailing and
leading spaces?

Eric B.
 

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