C# 程式拆分字串時使用空格


首先,設定一個字串 -

string str = "Science and Mathematics";

現在使用 Split() 方法在所有空格出現的地方進行拆分 -

str.Split(' ')

以下為完整程式碼 -

示例

 實況轉播版演示

using System;
using System.Linq;
using System.IO;
class Program {
   static void Main() {
      string str = "Science and Mathematics";
      Console.WriteLine("String...
"+str);       string[] myStr = str.Split(' ');       Console.WriteLine("
Splitted String...");       foreach (string ch in myStr) {          Console.WriteLine(ch);       }    } }

輸出

String...
Science and Mathematics

Splitted String...
Science
and
Mathematics

更新日期: 22-6 月-2020

2K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告