简体   繁体   中英

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 ??

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM