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

c# add spaces between words starting with capital letters

PHPz
PHPzforward
2023-09-24 14:29:05997browse

c# 在以大写字母开头的单词之间添加空格

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 -

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

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete