Tuesday, December 13, 2011

put spaces before capital letters in a string

From here I stole somewhat to come up with:

using System.Text.RegularExpressions;

namespace OurApp.Core.Utils

{

   public static class StringSpaceMaker

   {

      public static string AppendSpacesBeforeCapitalLetters(string value)

      {

         value = Regex.Replace(value, "[A-Z]", " $0");

         return value.Substring(1, value.Length - 1);

      }

   }

}

 
 

I got it under test like so:

using OurApp.Core.Utils;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace OurApp.Tests.Unit.Core.Utils

{

   [TestClass]

   public class StringSpaceMakerTest

   {

      [TestMethod]

      public void MayAppendSpacesBeforeCapitalLetters()

      {

         string value = "IThinkThereforeIAm";

         value = StringSpaceMaker.AppendSpacesBeforeCapitalLetters(value);

         Assert.AreEqual("I Think Therefore I Am", value);

      }

   }

}

 
 

Why do I need this? I'm going to use this to cast enum values where words AreJammedTogether to readable text painlessly.

No comments:

Post a Comment