tags:

views:

69

answers:

2

I'm trying to split a song title into two strings- the artist and song. I receive the original string like this: "artist - song". Using this code, I split the string using '-' as the spliiter:

    char[] splitter = { '-' };
    string[] songInfo = new string[2];
    songInfo = winAmp.Split(splitter);

This works fine and all, except when I get to a band with '-' in the name, like SR-71. However, since the original strings are separated with a space then a - and a space again (like SR-71 - Tomorrow), how would I split the string so this happens? I tried changing splitter to a string and inputting

        string[] splitter = { " - " };

in it, but it returns that there is no overload match.

+2  A: 

For some reason, string.Split has no overload that only takes a string array.

You need to call this overload:

string[] songInfo = winAmp.Split(new string[] { " - " }, StringSplitOptions.None);

Don't ask me why.

SLaks
Ah, thank you, this worked.
DMan
Can also shorten slightly using implicitly-typed array:new [] { " - " }
cxfx
Yes, but only in C# 3.
SLaks
A: 

You can also use

 Match M = System.Text.RegularExpressions.Regex.match(str,"(.*?)\s-\s(.*)");
 string Group = M.Groups[1].Value;
 string Song = M.Groups[2].Value; 
rerun