3

I have a string: CategoryName

I need to add space between Category and Name.

So I have to insert a space before each capital letter.

  • 2
    Regex may be too slow: http://stackoverflow.com/questions/272633/add-spaces-before-capital-letters?rq=1 – amnezjak Apr 16 '13 at 08:20
  • 1
    What about `SimpleHTTPRequest`? Do you really want that to become `Simple H T T P Request`? Shouldn't it be `Simple HTTP Request`? – Tim Pietzcker Apr 16 '13 at 08:24
  • My answer supports Tim's point, but also what should happen if there are numbers? e.g. `MyCategory1` or `MyCategory2Fish` – Bob Vale Apr 16 '13 at 08:32

3 Answers3

5
var input = "CategoryName";
var result = Regex.Replace(input, "([a-z])([A-Z])", @"$1 $2"); //Category Name

UPDATE (this will treat sequence of capital letters as one word)

var input = "SimpleHTTPRequest";
var result = Regex.Replace(input, "([a-z]|[A-Z]{2,})([A-Z])", @"$1 $2");
//Simple HTTP Request
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
4

This code will do the job

var source = "CategoryName";
var nameConvert = new Regex(@"((?<=[a-z])[A-Z]|(?<!^|\s)[A-Z][a-z])");
var converted = nameConvert.Replace(source, " $1");

This will leave multiple capital letters together e.g. FearTheCIAReally becomes Fear The CIA Really

To explain the regex:

  • ( start capture group $1
  • (?<=[a-z])[A-Z] capital letter preceded by a lower case letter (don't capture lower case)
  • | or
  • (?<!^|\s) preceding character not space or start of string, but don't capture
  • [A-Z] capital letter
  • [a-z] followed by a lower case letter
  • ) end capture group 1

I actually have this as a library function I use all the time

public static class StringExtensions {

   private static readonly Regex NameConvert =
                new Regex(@"((?<=[a-z])[A-Z]|(?<!^|\s)[A-Z][a-z])");

   public static string ToDisplayFormat(this string name) {
     return string.IsNullOrEmpty(name) ? 
       String.Empty :
       NameConvert.Replace(name," $1");
   }
}

And then I can just use it in code

var name="CategoryName";
var displayName = name.ToDisplayFormat();
Ahmed Subhani
  • 45
  • 1
  • 6
Bob Vale
  • 18,094
  • 1
  • 42
  • 49
  • +1 from me, that should be an answer! – Sergey Berezovskiy Apr 16 '13 at 08:52
  • BTW looks like you don't need last group for lower case letters - you can simply use `((?<=[a-z])[A-Z]|(?<!^|\s)[A-Z][a-z])`. But answer anyway very nice – Sergey Berezovskiy Apr 16 '13 at 09:05
  • 1
    @lazyberezovsky Good point, I was so busy concentrating on making sure I only matched the capital letter that I didn't consider that it's ok to include the following lowercase because the match works using look behind. I'll update – Bob Vale Apr 16 '13 at 09:13
  • @lazyberezovsky Any reason you removed the readonly on the field? – Bob Vale Apr 16 '13 at 09:14
  • @BobValue I was adding variable type name, that you missed. And new string `private static readonly Regex` was too long, so I removed non important part to get rid of slider from code window - it's not convenient to scroll to the right to see the code (actually most important part of it). You can split assignment in two lines, if you wish – Sergey Berezovskiy Apr 16 '13 at 09:19
0
([A-Z?])[_ ]?([a-z])

Try this Regular expression.

Hope it helps.

Freelancer
  • 9,008
  • 7
  • 42
  • 81