views:

2449

answers:

11

What is the best way to convert from Pascal Case (upper Camel Case) to a sentence.

For example starting with

"AwaitingFeedback"

and converting that to

"Awaiting feedback"

C# preferable but I could convert it from Java or similar.

A: 

Pseudo-code:

NewString = "";
Loop through every char of the string (skip the first one)
   If char is upper-case ('A'-'Z')
     NewString = NewString + ' ' + lowercase(char)
   Else
     NewString = NewString + char

Better ways can perhaps be done by using regex or by string replacement routines (replace 'X' with ' x')

schnaader
A: 

I'd use a regex, inserting a space before each upper case character, then lowering all the string.

    string spacedString = System.Text.RegularExpressions.Regex.Replace(yourString, "\B([A-Z])", " \k");
    spacedString = spacedString.ToLower();
Antoine
I don't know C# but I don't think escapes like \s are legal in the replace section: how the language will know if it must insert space, tab or something else? :-)
PhiLho
You're right, should be simpler to replace it with a clear " ".
Antoine
The only thing I'd say is that will produce "awaiting feedback"
Garry Shutler
Well you sure have to remove the first space and upper the first character. Maybe add a "\B" before the pattern so as not to match the first char.
Antoine
+9  A: 

Here you go...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CamelCaseToString
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(CamelCaseToString("ThisIsYourMasterCallingYou"));   
        }

        private static string CamelCaseToString(string str)
        {
            if (str == null || str.Length == 0)
                return null;

            StringBuilder retVal = new StringBuilder(32);

            retVal.Append(char.ToUpper(str[0]));
            for (int i = 1; i < str.Length; i++ )
            {
                if (char.IsLower(str[i]))
                {
                    retVal.Append(str[i]);
                }
                else
                {
                    retVal.Append(" ");
                    retVal.Append(char.ToLower(str[i]));
                }
            }

            return retVal.ToString();
        }
    }
}
SDX2000
You should ToUpper() the first character, otherwise your routine doesn't work with true camelCase, only PascalCase
Ch00k
Yup nice catch, I knew something was amiss (but couldn't put my finger on it) when he said "AwaitingFeedback" is camel case!
SDX2000
A: 

http://letmegooglethatforyou.com/?q=regex+camel+case

top link

spender
I think you'll find that doesn't do what I want. I also apologise for asking a question that, though basic, I thought would be useful to have on SO. Therefore, I asked it whilst working out a solution.
Garry Shutler
Perfectly ok to ask such a question I believe. Also, what will be the top link in google for this in one year? next week?
Mario Ortegón
It's an easy thing to search for with a standard search engine turning up plenty of prior art, regardless of whether or not the top link stays live. I wonder if the questioner would have needed to pose the question after a cursory search. Apologies for seeming grumpy!
spender
+1  A: 

It is easy to do in JavaScript (or PHP, etc.) where you can define a function in the replace call:

var camel = "AwaitingFeedbackDearMaster";
var sentence = camel.replace(/([A-Z].)/g, function (c) { return ' ' + c.toLowerCase(); });
alert(sentence);

Although I haven't solved the initial cap problem... :-)

Now, for the Java solution:

String ToSentence(String camel)
{
  if (camel == null) return ""; // Or null...
  String[] words = camel.split("(?=[A-Z])");
  if (words == null) return "";
  if (words.length == 1) return words[0];
  StringBuilder sentence = new StringBuilder(camel.length());
  if (words[0].length() > 0) // Just in case of camelCase instead of CamelCase
  {
    sentence.append(words[0] + " " + words[1].toLowerCase());
  }
  else
  {
    sentence.append(words[1]);
  }
  for (int i = 2; i < words.length; i++)
  {
    sentence.append(" " + words[i].toLowerCase());
  }
  return sentence.toString();
}

System.out.println(ToSentence("AwaitingAFeedbackDearMaster"));
System.out.println(ToSentence(null));
System.out.println(ToSentence(""));
System.out.println(ToSentence("A"));
System.out.println(ToSentence("Aaagh!"));
System.out.println(ToSentence("stackoverflow"));
System.out.println(ToSentence("disableGPS"));
System.out.println(ToSentence("Ahh89Boo"));
System.out.println(ToSentence("ABC"));

Note the trick to split the sentence without loosing any character...

PhiLho
+2  A: 

Here's a basic way of doing it that I came up with using Regex

public static string CamelCaseToSentence(this string value)
{
    var sb = new StringBuilder();
    var firstWord = true;

    foreach (var match in Regex.Matches(value, "([A-Z][a-z]+)|[0-9]+"))
    {
        if (firstWord)
        {
            sb.Append(match.ToString());
            firstWord = false;
        }
        else
        {
            sb.Append(" ");
            sb.Append(match.ToString().ToLower());
        }
    }

    return sb.ToString();
}

It will also split off numbers which I didn't specify but would be useful.

Garry Shutler
A: 
string camel = "MyCamelCaseString";
string s = Regex.Replace(camel, "([A-Z])", " $1").ToLower().Trim();
Console.WriteLine(s.Substring(0,1).ToUpper() + s.Substring(1));

Edit: didn't notice your casing requirements, modifed accordingly. You could use a matchevaluator to do the casing, but I think a substring is easier. You could also wrap it in a 2nd regex replace where you change the first character

"^\w"

to upper

\U (i think)
Andrew Bullock
A: 

Mostly already answered here

Small chage to the accepted answer, to convert the second and subsequent Capitalised letters to lower case, so change

if (char.IsUpper(text[i]))                
    newText.Append(' ');            
newText.Append(text[i]);

to

if (char.IsUpper(text[i]))                
{
    newText.Append(' ');            
    newText.Append(char.ToLower(text[i]));
}
else
   newText.Append(text[i]);
Binary Worrier
+4  A: 
public static string ToSentenceCase(this string str)
{
    return Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + " " + char.ToLower(m.Value[1]));
}

Based on: Converting Pascal case to sentences using regular expression

Nice simple solution, thanks +1
Mark Cooper
A: 

An xquery solution that works for both UpperCamel and lowerCamel case:

To output sentence case (only the first character of the first word is capitalized):

declare function content:sentenceCase($string) { let $firstCharacter := substring($string, 1, 1) let $remainingCharacters := substring-after($string, $firstCharacter) return concat(upper-case($firstCharacter),lower-case(replace($remainingCharacters, '([A-Z])', ' $1'))) };

To output title case (first character of each word capitalized):

declare function content:titleCase($string) { let $firstCharacter := substring($string, 1, 1) let $remainingCharacters := substring-after($string, $firstCharacter) return concat(upper-case($firstCharacter),replace($remainingCharacters, '([A-Z])', ' $1')) };

Fraser
+1  A: 

This works for me:

Regex.Replace(strIn, "([A-Z]{1,2}|[0-9]+)", " $1").TrimStart()
SSTA
Nice and short, sweet!
Philippe Monnet