2

In my example var key = new CultureInfo("en-GB").TextInfo.(item.Key) produces, 'Camelcase' what regular expression could I add that would produce a space before the second 'c' ?

Examples:

'camelCase' > 'Camel case'

'itIsTimeToStopNow' > 'It is time to stop now'

Andy
  • 454
  • 2
  • 7
  • 22

2 Answers2

9

One of the ways how you can do it.

string input = "itIsTimeToStopNow";
string output = Regex.Replace(input, @"\p{Lu}", m => " " + m.Value.ToLowerInvariant());
output = char.ToUpperInvariant(output[0]) + output.Substring(1);
Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31
3

One way is to replace capital letters with space capital letters then make the first character uppercase:

var input = "itIsTimeToStopNow";

// add spaces, lower case and turn into a char array so we 
// can manipulate individual characters
var spaced = Regex.Replace(input, @"[A-Z]", " $0").ToLower.ToCharArray();

// spaced = { 'i', 't', ' ', 'i', 's', ' ', ... }

// replace first character with its uppercase equivalent
spaced[0] = spaced[0].ToString().ToUpper()[0];

// spaced = { 'I', 't', ' ', 'i', 's', ' ', ... }

// combine the char[] back into a string
var result = String.Concat(spaced);

// result = "It is time to stop now"
dav_i
  • 27,509
  • 17
  • 104
  • 136
  • This answer works perfectly, it was first and had explanatory notes so I have up voted it. however the other solution seemed to use a bit less code when I integrated it into my page. – Andy Nov 20 '14 at 13:48