Siddharth Koya
Siddharth Koya

Reputation: 103

String Split in C# in this Particular Manner

Guys I am Fetching Data From Following Query

MySqlCommand cmd = new MySqlCommand("select concat(c1.countryname,' vs ',c2.countryname) as CountryACountryB,r.Description as Round,m.result,m.matchId from matches m left join countries c1 on m.countryId1=c1.countryId left join countries c2 on m.countryId2=c2.countryId left join rounds r on m.roundId=r.roundId where matchId=" + msMatch.SelectedItem.Value, register);

When i check i Value coming in CountryACountryB its Argentina Vs Bosnia and Herzegovina(For Eg:)

This is the code i implemented for splitting the String

string fullTeam = dr["CountryACountryB"].ToString();
            string[] names = fullTeam.Split(' ', '-');
            string name = names.First();
            string lasName = names.Last();
            string Final = name + ". " + lasName;

It does the Job but when it comes to Argentina vs Bosnia and Herzegovina

string name = names.First();//Comes as Argentina(Which is Fine)
string lasName = names.Last();//Comes Herzegovina

This is the way i want it

string Final = name + ". " + lasName;//Argentina + ". " + Bosnia and Herzegovina

Any help guys ??

Upvotes: 0

Views: 140

Answers (2)

Rimpy
Rimpy

Reputation: 888

As you are concatenation with ' vs '. You should use same string with to split.

So use

string[] names = fullTeam.Split(new string[]{" vs "}, StringSplitOptions.None);

My personal opinion is that You don't need to concatenate. Just access countryA and countryB separately

Upvotes: 4

Grozz
Grozz

Reputation: 8425

fullTeam.Split(' ', '-') -> fullTeam.Split(new[]{" vs "}, StringSplitOptions.None);

Upvotes: 2

Related Questions