C#程式分割和連線字串


若要使用C#分割和連線一個字串,請使用split()和join()方法。假設以下內容是我們的字串-

string str = "This is our Demo String";

若要分割字串,我們將使用split()方法-

var arr = str.Split(' ');

現在,若要連線字串,請使用join()方法,並連線字串的其餘部分。這裡,我們跳過了字串的部分,使用了skip()方法-

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

示例

您可以嘗試執行以下C#程式碼來分割和連線一個字串。

線上演示

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

輸出

is our Demo String

更新日期:2020年6月19日

2K + 瀏覽量

開啟您的職業生涯

完成課程並獲得認證

開始
廣告