c# Put spaces between words starting with capital letters


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();
   }
}

Updated on: 21-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements