How do I go from this string: "ThisIsMyCapsDelimitedString"
...to this string: "This Is My Caps Delimited String"
Fewest lines of code in VB.net is preferred but C# is also welcome.
Cheers!
How do I go from this string: "ThisIsMyCapsDelimitedString"
...to this string: "This Is My Caps Delimited String"
Fewest lines of code in VB.net is preferred but C# is also welcome.
Cheers!
There's probably a more elegant solution, but this is what I come up with off the top of my head:
string myString = "ThisIsMyCapsDelimitedString";
for (int i = 1; i < myString.Length; i++)
{
     if (myString[i].ToString().ToUpper() == myString[i].ToString())
     {
          myString = myString.Insert(i, " ");
          i++;
     }
}
Grant Wagner's excellent comment aside:
Dim s As String = RegularExpressions.Regex.Replace("ThisIsMyCapsDelimitedString", "([A-Z])", " $1")
string s = "ThisIsMyCapsDelimitedString";
string t = Regex.Replace(s, "([A-Z])", " $1").Substring(1);
Naive regex solution. Will not handle O'Conner, and adds a space at the start of the string as well.
s = "ThisIsMyCapsDelimitedString"
split = Regex.Replace(s, "[A-Z0-9]", " $&");
Just for a little variety... Here's an extension method that doesn't use a regex.
public static class CamelSpaceExtensions
{
    public static string SpaceCamelCase(this String input)
    {
        return new string(InsertSpacesBeforeCaps(input).ToArray());
    }
    private static IEnumerable<char> InsertSpacesBeforeCaps(IEnumerable<char> input)
    {
        foreach (char c in input)
        {
            if (char.IsUpper(c)) 
            { 
                yield return ' '; 
            }
            yield return c;
        }
    }
}
I made this a while ago. It matches each component of a CamelCase name.
/([A-Z]+(?=$|[A-Z][a-z])|[A-Z]?[a-z]+)/g
For example:
"SimpleHTTPServer" => ["Simple", "HTTP", "Server"]
"camelCase" => ["camel", "Case"]
To convert that to just insert spaces between the words:
Regex.Replace(s, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 ")
Edit: Allowing initial lowercase letters, (i.e. "lowerCamelCase"), as Drew Noakes pointed out. The only change is a "?" after after the last "[A-Z]".
For more variety, using plain old C# objects, the following produces the same output as @MizardX's excellent regular expression.
public string FromCamelCase(string camel)
{   // omitted checking camel for null
    StringBuilder sb = new StringBuilder();
    int upperCaseRun = 0;
    foreach (char c in camel)
    {   // append a space only if we're not at the start
        // and we're not already in an all caps string.
        if (char.IsUpper(c))
        {
            if (upperCaseRun == 0 && sb.Length != 0)
            {
                sb.Append(' ');
            }
            upperCaseRun++;
        }
        else if( char.IsLower(c) )
        {
            if (upperCaseRun > 1) //The first new word will also be capitalized.
            {
                sb.Insert(sb.Length - 1, ' ');
            }
            upperCaseRun = 0;
        }
        else
        {
            upperCaseRun = 0;
        }
        sb.Append(c);
    }
    return sb.ToString();
}
Great answer, MizardX! I tweaked it slightly to treat numerals as separate words, so that "AddressLine1" would become "Address Line 1" instead of "Address Line1":
Regex.Replace(s, "([a-z](?=[A-Z0-9])|[A-Z](?=[A-Z][a-z]))", "$1 ")