摘要:String.Split字串分割(語法C#)
開一個新的網頁命名為
WebForm2
未修改前:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebTestC
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str1 = "這|||||是|||||字|||||串|||||分|||||割|||||範|||||例";
string[] str2 = null;
str1 = str1.Replace("|||||", "|");
str2 = str1.Split('|');
foreach (string i in str2)
{
Response.Write(i.ToString());
}
}
}
}
經darkthread大大建議後所做的修改:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
namespace WebTestC
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] str1 = Regex.Split("這|||||是|||||字|||||串|||||分|||||割|||||範|||||例", @"\|{5}");
foreach (string i in str1)
{
Response.Write(i.ToString());
}
}
}
}
完成結果