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

更新於:19-6-2020

2K+ 次瀏覽

開啟您的 職業

完成課程,獲得認證

開始
廣告
© . All rights reserved.