c# Put spaces between words starting with capital letters

CsharpProgrammingServer Side Programming

To place spaces in between words starting with capital letters, try the following example −

Firstly, set the string.

var str = "WelcomeToMyWebsite";

As you can see above, our string isn’t having spaces before capital letter. To add it, use LINQ as we have done below −

str = string.Concat(str.Select(x => Char.IsUpper(x) ? " " + x : x.ToString())).TrimStart(' ');

The following is the complete code to place spaces between words beginning with capital letters −

Example

using System;
using System.Linq;

class Demo {

   static void Main() {
      var str = "WelcomeToMyWebsite";

      Console.WriteLine("Original String: "+str);
      str = string.Concat(str.Select(x => Char.IsUpper(x) ? " " + x : x.ToString())).TrimStart(' ');

      Console.WriteLine("New String: "+str);
      Console.ReadLine();
   }
}
raja
Published on 14-Aug-2018 13:02:17
Advertisements