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
廣告