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

C# program to split and concatenate strings

王林
王林forward
2023-08-28 23:13:101259browse

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";

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

var arr = str.Split(' ');

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

Example

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

Real-time demonstration

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(' ');
         // skips the first element and joins rest of the array
         string rest = string.Join(" ", arr.Skip(1));
         Console.WriteLine(rest);
      }
   }
}

Output

is our Demo String

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!

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