Function that like StringTokenizer in C#
Question: 我有一个字符串,我想以空格把里面的单词分开,以前在Java中有StringTokenizer很方便,不知C#中有没有类似的.
using System;
using System.Text.RegularExpressions;
class SplitTest
{
public static void Main()
{
String s = "Hello, Houston,I,am,coming";
Console.WriteLine("First example:");
Console.WriteLine("Original stirng:" + s);
Console.WriteLine("After splitting:");
String[] tokens = Regex.Split(s,",");
for (int i=0; i tokens.Length; i++)
Console.WriteLine(tokens[i]);
s = "Hello | Houston | I | am | coming";
Console.WriteLine("Second example:");
Console.WriteLine("Original stirng:" + s);
Console.WriteLine("After splitting:");
tokens = Regex.Split(s,@"\s*\|\s*");
for (int i=0; i tokens.Length; i++)
Console.WriteLine(tokens[i]);
}
}
Question: 我有一个字符串,我想以空格把里面的单词分开,以前在Java中有StringTokenizer很方便,不知C#中有没有类似的.
using System;
using System.Text.RegularExpressions;
class SplitTest
{
public static void Main()
{
String s = "Hello, Houston,I,am,coming";
Console.WriteLine("First example:");
Console.WriteLine("Original stirng:" + s);
Console.WriteLine("After splitting:");
String[] tokens = Regex.Split(s,",");
for (int i=0; i tokens.Length; i++)
Console.WriteLine(tokens[i]);
s = "Hello | Houston | I | am | coming";
Console.WriteLine("Second example:");
Console.WriteLine("Original stirng:" + s);
Console.WriteLine("After splitting:");
tokens = Regex.Split(s,@"\s*\|\s*");
for (int i=0; i tokens.Length; i++)
Console.WriteLine(tokens[i]);
}
}
powered by performancing firefox
No comments:
Post a Comment