private string FormatName(string name, string arg)
{
// Break into separate words
string[] parts = name.Split(' ');
// Characters that can be used as separators, if there is one of these at the start
// or end of a part then it won't put the arg character next to it. This is to avoid
// outputs like Day.1.12.00.-.1.00 for a 24 episode since .-. looks silly.
// Or names with titles in like Mr. Smith would be Mr..Smith - equally silly.
var separatorChars = new[] { '-', ':', '_', '.', ',' };
// The new name to be returned
string newName = parts[0];
// Loop through each part appending it with the appropriate separator
for (int i = 1; i < parts.Length; i++)
{
// Check for the separator chars at the start of this part of the end of the next.
bool prefix =
separatorChars.Select(x => x.ToString(CultureInfo.InvariantCulture)).All(
sep => !parts[i].StartsWith(sep) && !parts[i - 1].EndsWith(sep));
// If there is already a prefix then don't add another
// Add the arg character
if (prefix)
{
newName += arg;
}
// Add the part
newName += parts[i];
}
// If the string ends with a separator character then remove it
newName = newName.TrimEnd(separatorChars);
return newName;
}