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+ 浏览量

开启你的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.