程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 使用C#自寫的SPLIT函數的代碼

使用C#自寫的SPLIT函數的代碼

編輯:關於C#

因為C#的SPLIT局限性比較大,只能用字符或字符數組來做分隔符。沒有提供像VB一樣的可以用字符串做分割符的SPLIT函數,這樣在寫程序過程中,少了很多便利。

於是,就寫了如下一個函數來方便自己編程。

程序如下:

=====================================================
using System;
using System.Collections;
namespace split
...{
class mySplit
...{
[STAThread]
static void Main(string[] args)
...{
string str1="",str2="";
if(args.Length != 2)
...{
Console.WriteLine("請輸入要分割的字符串:");
str1 = Console.ReadLine();
Console.WriteLine("請輸入分割符:");
str2 = Console.ReadLine();
Console.WriteLine(" 分割出的數據如下: ");
}
else
...{
str1 = args[0];
str2 = args[1];
}
string[] output = null;
output = split(str1,str2);
for (int i=0; i < output.Length; i++)
...{
Console.WriteLine("{0}", output[i]);
}
Console.WriteLine(" 被分割成{0}份字符串!", output.Length);
TimeSpan sp1,sp2,sp3;
DateTime dt = DateTime.Now;
for(int i=0; i<1000; i++)
...{
output=split(str1,str2);
output=null;
}
sp1=DateTime.Now - dt;
//用&&符分割!
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex("&{2}");
dt = DateTime.Now;
for(int j=0; j<1000; j++)
...{
output=rg.Split(str1);
output=null;
}
sp2=DateTime.Now - dt;
Console.WriteLine(" 正則式類執行

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved