Home > Backend Development > C#.Net Tutorial > C# program to split and concatenate strings

C# program to split and concatenate strings

王林
Release: 2023-08-28 23:13:10
forward
1255 people have browsed it

C# 程序来分割和连接字符串

To split and join strings in C#, use the split() and join() methods. Suppose the following is our string -

string str = "This is our Demo String";
Copy after login

To split the string we will use split() method -

var arr = str.Split(' ');
Copy after login

Now to join use join() method and join the strings The rest. Here we are skipping a part of the string using skip() method -

string rest = string.Join(" ", arr.Skip(1));
Copy after login

Example

You can try running the following code in C# to split and concatenate the string.

Real-time demonstration< /p>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
   class MyApplication {
      static void Main(string[] args) {
         string str = "This is our Demo String";
         var arr = str.Split(&#39; &#39;);
         // skips the first element and joins rest of the array
         string rest = string.Join(" ", arr.Skip(1));
         Console.WriteLine(rest);
      }
   }
}
Copy after login

Output

is our Demo String
Copy after login

The above is the detailed content of C# program to split and concatenate strings. 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