如何:使用 Split 方法分析字符串

更新:2007 年 11 月

下面的代码示例演示如何使用 String.Split 方法从字符串中提取每个字。首先构造包含多种类型的字描绘器的字符串,然后使用一个描绘器列表调用 Split 来分析该字符串。然后,分别显示句子中的每个字。

示例

// regex_split.cpp
// compile with: /clr
using namespace System;

int main()
{
   String^ delimStr = " ,.:\t";
   Console::WriteLine( "delimiter : '{0}'", delimStr );
   array<Char>^ delimiter = delimStr->ToCharArray( );
   array<String^>^ words;
   String^ line = "one\ttwo three:four,five six seven";

   Console::WriteLine( "text : '{0}'", line );
   words = line->Split( delimiter );
   Console::WriteLine( "Number of Words : {0}", words->Length );
   for (int word=0; word<words->Length; word++)
      Console::WriteLine( "{0}", words[word] );

   return 0;
}

请参见

其他资源

.NET Framework 正则表达式

.NET 编程指南