Home > Backend Development > C#.Net Tutorial > c# add spaces between words starting with capital letters

c# add spaces between words starting with capital letters

PHPz
Release: 2023-09-24 14:29:05
forward
1089 people have browsed it

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

To put spaces between words that start with a capital letter, try the following example -

First, set up the string.

var str = "WelcomeToMyWebsite";
Copy after login

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(' ');
Copy after login

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template