1

I have following strings (I'm dealing here with the names of Tennisplayers):

string bothPlayers = "N. Djokovic - R. Nadal"; //works with my code
string bothPlayer2 = "R. Federer - G. Garcia-Lopez"; //works with my code 
string bothPlayer3 = "G. Garcia-Lopez - R. Federer"; //doesnt works
string bothPlayer4 = "E. Roger-Vasselin - G. Garcia-Lopez"; //doesnt works

My aim is to get these both players, separated in two new strings (for the first bothPlayers it would be):

string firstPlayer = "N. Djokovic";
string secondPlayer = "R. Nadal";

What I tried

I solved, to split the first string/bothPlayers with the following method (also explains why I put "works" as a comment behind).. The Second also works, but its just luck as I'm searchin for the first '-' and split then.. But I'm not able to make it work for all 4 cases.. Here's my Method:

string bothPlayers = "N. Djokovic - R. Nadal"; //works 
string bothPlayer2 = "R. Federer - G. Garcia-Lopez"; //works
string bothPlayer3 = "G. Garcia-Lopez - R. Federer"; //doesnt works
string bothPlayer4 = "E. Roger-Vasselin - G. Garcia-Lopez"; //doesnt works

string firstPlayerName = String.Empty;
string secondPlayerName = String.Empty;

int index = -1;
int countHyphen = bothPlayers.Count(f=> f == '-'); //Get Count of '-' in String

index = GetNthIndex(bothPlayers, '-', 1);
if (index > 0)
{
    firstPlayerName = bothPlayers.Substring(0, index).Trim();
    firstPlayerName = firstPlayerName.Trim();

    secondPlayerName = bothPlayers.Substring(index + 1, bothPlayers.Length - (index + 1));
    secondPlayerName = secondPlayerName.Trim();

    if (countHyphen == 2)
    {
        //Maybe here something?..
    }
}

//Getting the Index of a specified character (Here for us: '-') 
public int GetNthIndex(string s, char t, int n)
{
    int count = 0;
    for (int i = 0; i < s.Length; i++)
    {
        if (s[i] == t)
        {
            count++;
            if (count == n)
            {
                return i;
            }
        }
    }
    return -1;
}

Maybe someone could help me.

Marc
  • 16,170
  • 20
  • 76
  • 119
eMi
  • 5,540
  • 10
  • 60
  • 109
  • 3
    Any reason you can't use `string.Split(new string[] {" - "})`? – Oded Oct 18 '12 at 13:18
  • No theres no reason.. Could use anything.. I'm just not that good in solving such String-Manipulation problems :)) – eMi Oct 18 '12 at 13:19
  • @Oded, doesn't `string.Split` have an array of chars, instead of a string by which it splits? – Bart Friederichs Oct 18 '12 at 13:19
  • @BartFriederichs - It has multiple overloads, but you are right, I was using the incorrect one (as for the chars overload - you can pass in a single char to as it is implemented as a `params char[]`). – Oded Oct 18 '12 at 13:21

2 Answers2

8

Most of your code can be replaced by using the built in string.Split methods:

var split = "N. Djokovic - R. Nadal".Split(new string[] {" - "}, 
                                           StringSplitOptions.None);

string firstPlayer = split[0];
string secondPlayer = split[1];
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • that works in my case also.. But doesn't fits all Games :) .. How u deal with: "E. Roger-Vasselin - G. Garcia-Lopez" ?? There are 3 '-' – eMi Oct 18 '12 at 13:21
  • 2
    It does work because there is a difference between `'-'`and `' - '` in the split – KyorCode Oct 18 '12 at 13:22
  • Oh.. I actually didn't think about (spaces).. Ok Thanks :) – eMi Oct 18 '12 at 13:23
  • @eMi: Its the basic lesson in program debugging that you should have good observation and check closely why its not working... – techBeginner Oct 18 '12 at 13:26
0

"string".Split can be used, but you'll have to supply an array of strings, the easiest way would be the following:

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

string firstPlayer = names[0];
string secondPlayer = names[1];

Good luck :)

Destrictor
  • 752
  • 1
  • 4
  • 15