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
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP