5

I may have just hit the point where i;m overthinking it, but I'm wondering: is there a way to designate a list of special characters that should all be considered delimiters, then splitting a string using that list? Example:

"battlestar.galactica-season 1"

should be returned as

battlestar galactica season 1

i'm thinking regex but i'm kinda flustered at the moment, been staring at it for too long.

EDIT: Thanks guys for confirming my suspicion that i was overthinking it lol: here is what i ended up with:

//remove the delimiter
            string[] tempString = fileTitle.Split(@"\/.-<>".ToCharArray());
            fileTitle = "";
            foreach (string part in tempString)
            {
                fileTitle += part + " ";
            }

            return fileTitle;

I suppose i could just replace delimiters with " " spaces as well... i will select an answer as soon as the timer is up!

Sinaesthetic
  • 11,426
  • 28
  • 107
  • 176

7 Answers7

7

The built-in String.Split method can take a collection of characters as delimiters.

string s = "battlestar.galactica-season 1";
string[] words = s.split('.', '-');
mellamokb
  • 56,094
  • 12
  • 110
  • 136
2

The standard split method does that for you. It takes an array of characters:

public string[] Split(
    params char[] separator
)
Ken Pespisa
  • 21,989
  • 3
  • 55
  • 63
2

You can just call an overload of split:

myString.Split(new char[] { '.', '-', ' ' }, StringSplitOptions.RemoveEmptyEntries);

The char array is a list of delimiters to split on.

Tejs
  • 40,736
  • 10
  • 68
  • 86
1
"battlestar.galactica-season 1".Split(new string[] { ".", "-" }, StringSplitOptions.RemoveEmptyEntries);
abatishchev
  • 98,240
  • 88
  • 296
  • 433
pjwilliams
  • 300
  • 2
  • 10
0

For more information split with easy examples you may see following Url:

This also include split on words (multiple chars). C# Split Function explained

0

This may not be complete but something like this.

string value = "battlestar.galactica-season 1"

char[] delimiters = new char[] { '\r', '\n', '.', '-' };
    string[] parts = value.Split(delimiters,
                     StringSplitOptions.RemoveEmptyEntries);
    for (int i = 0; i < parts.Length; i++)
    {
        Console.WriteLine(parts[i]);
    }
Priyank
  • 10,503
  • 2
  • 27
  • 25
0

Are you trying to split the string (make multiple strings) or do you just want to replace the special characters with a space as your example might also suggest (make 1 altered string). For the first option just see the other answers :)

If you want to replace you could use

string title = "battlestar.galactica-season 1".Replace('.', ' ').Replace('-', ' ');
Jan-Peter Vos
  • 3,157
  • 1
  • 18
  • 21
  • for some reason every version of replace that ive tried isn't working... stuck with the word array :/ – Sinaesthetic May 12 '11 at 20:06
  • I slightly changed my example to reflect the need to re-assign the result of replace. If however you are looking for an array in the technical meaning of the word my example might be of little use for you and you should use any of the other answers posted here. – Jan-Peter Vos May 12 '11 at 20:24