1

I need to split a string describing a soccer match into home team and away team.

The string will always be something like "Manchester United v Liverpool".

I need to split on ' v ' - i know that - I just don't know how to do it in C#

I tried

 string s = item.Summary;
 string[] teams = s.Split('v');
 tempEvent.HomeTeam = teams[0].Trim();
 tempEvent.AwayTeam = teams[1].Trim();

but naturally that made the above mentioned game be between Manchester United and Li

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
catu
  • 888
  • 6
  • 24

1 Answers1

5

Try this code:

string game = "Manchester United v Liverpool";
string[] teams = game.Split(new[] { " v " }, StringSplitOptions.None);

It will split the string on [interval]v[interval].

So if game is Manchester United v Liverpool the two strings in teams will be Manchester United and Liverpool.

enter image description here

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123