Split()函数

最近遇到一个有趣的问题关于使用Split函数 ,该函数能够根据传递的参数拆分,并返回一个string的数组。

贴出一个奇怪的例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace voucher.AutoCreatXml.Code
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string str = "887766555444333";

            int number1 = str.Split('8').Length-1;
            int a = 8;
            int number2 = str.Split(Convert.ToChar(a)).Length - 1;

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"","alert('number1:"+number1+"***number2:"+number2+"')",true);
        }
    }
}

输出的结果
难道Convert.ToChar()不能转换?赶紧调试了一下  ,发现

难怪number1和Number2的值不一样。仔细一想原来是int number1 = str.Split('8').Length-1;  这个8 是个char字符,然而int a = 8;int number2 = str.Split(Convert.ToChar(a)).Length - 1; 这个是将int类型的8转化为字符串,当然不一样了  '8' 是char字符串unicode码

https://msdn.microsoft.com/zh-cn/library/x9h8tsay.aspx有关char类型

原文地址:https://www.cnblogs.com/yuanyanyan/p/6677580.html