Parse Sting:  Add space everywhere string goes from capital letter to lowercase letter

Posted on 2007-10-19
Last Modified: 2012-06-21
I have a web service object that returns an enumertion as it's return message.  The values are strings.  I want a function to add a space everywhere the string goes from a capital letter to a lowercase.  For instance i want this EmailAlreadyRegistered to be Email Already Registered.
Question by:davidcahan
    1 Comment
    LVL 5

    Accepted Solution

    Hello, here's a function that will do what you want.

    > AddSpacesOnCaps("EmailAlreadyRegistered", true)
    "Email Already Registered"

    > AddSpacesOnCaps("ThisIsATest", true)
    "This Is A Test"

    > AddSpacesOnCaps("ThisIsATest", false)
    "This Is ATest"

          /// <summary>
          /// Adds a space after lowercase letters when followed by a capital letter.
          /// </summary>
          /// <param name="text">The input text.</param>
          /// <param name="addSpacesOnDoubleCaps">If true it will separate sequential capital letters with spaces as well.</param>
          /// <returns>The new string.</returns>
          public static string AddSpacesOnCaps(string text, bool addSpacesOnDoubleCaps)
                if (text != null)
                      System.Text.StringBuilder sb = new System.Text.StringBuilder();

                      bool prevIsLower = false;
                      for (int i = 0; i < text.Length; i++)
                            if ((prevIsLower || (addSpacesOnDoubleCaps && i > 0)) && char.IsUpper(text[i]))
                                  sb.Append(" ");
                                  prevIsLower = false;
                                  if (char.IsLower(text[i]))
                                        prevIsLower = true;

                      return (sb.ToString());
                      return (null);

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video discusses moving either the default database or any database to a new volume.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now