Char.IsUpper(x)?""+x:x.ToString())).TrimStart('') ;The following puts spaces between words starting with a capital letter"/> Char.IsUpper(x)?""+x:x.ToString())).TrimStart('') ;The following puts spaces between words starting with a capital letter">
Home > Article > Backend Development > c# add spaces between words starting with capital letters
To put spaces between words that start with a capital letter, try the following example -
First, set up the string.
var str = "WelcomeToMyWebsite";
As you can see above, our string has no spaces before capital letters. To add it, use LINQ as shown below -
str = string.Concat(str.Select(x => Char.IsUpper(x) ? " " + x : x.ToString())).TrimStart(' ');
Here is the complete code to put spaces between words starting with capital letters -
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(); } }
The above is the detailed content of c# add spaces between words starting with capital letters. For more information, please follow other related articles on the PHP Chinese website!