分割字符串函数split(C#)

        /// <summary>
        /// 字符串分割符
        /// </summary>
        public const string SEPARATORSTRING = "@#$@#$";

        /// <summary>
        /// 取得字符的第几个字符,通过分割符分割的字符串,pos以1开始
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="separator">分割符</param>
        /// <param name="pos">第几个</param>
        /// <returns>返回第几个字符串</returns>
        /// <example>string str = Split("rrrrddrew@@tqwewerewddccc", "@@", 1);</example>
        public static string Split(string str, string separator, int pos)
        {
            int Len = str.Length;
            int Count = str.Length - str.Replace(separator, "").Length;
            int BitCount = 0;
            string returnValue = "";
            string strValue = "";
            string InSeparator = "";
            if (Count == 0)
            {
                return str;
            }
            InSeparator = str.Substring(str.Length - separator.Length, separator.Length);
            if (InSeparator != separator)
            {
                str += separator;
            }

            for (int i = 0; i < Count; i++)
            {
                BitCount = str.IndexOf(separator, 0);
                strValue = str.Substring(0, BitCount);
                str = str.Replace(strValue + separator, "");
                if (i == pos - 1)
                {
                    returnValue = strValue;
                    break;
                }
            }
            return returnValue;
        }
        /// <summary>
        /// 取得字符的第几个字符,通过分割符分割的字符串,pos以1开始,以默认的字符串分割
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="pos">第几个</param>
        /// <returns>返回第几个字符串</returns>
        /// <example>string str = Split("rrrrddrew@@tqwewerewddccc",1);</example>
        public static string Split(string str, int pos)
        {
            int Len = str.Length;
            int Count = str.Length - str.Replace(SEPARATORSTRING, "").Length;
            int BitCount = 0;
            string returnValue = "";
            string strValue = "";
            string InSeparator = "";
            if (Count == 0)
            {
                return str;
            }
            InSeparator = str.Substring(str.Length - SEPARATORSTRING.Length, SEPARATORSTRING.Length);
            if (InSeparator != SEPARATORSTRING)
            {
                str += SEPARATORSTRING;
            }

            for (int i = 0; i < Count; i++)
            {
                BitCount = str.IndexOf(SEPARATORSTRING, 0);
                strValue = str.Substring(0, BitCount);
                str = str.Replace(strValue + SEPARATORSTRING, "");
                if (i == pos - 1)
                {
                    returnValue = strValue;
                    break;
                }
            }
            return returnValue;
        }
        /// <summary>
        /// 取得分割后的字符串数据,数组以0开始
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="separator">分割符</param>
        /// <returns>返回分割后的字符串数组</returns>
        /// <example>string str=Split("rrrrddrew@@tqwewerewddccc@@", "@@")[1].ToString()</example>
        public static string[] Split(string str, string separator)
        {
            int Len = str.Length;
            int Count = 0;
            int BitCount = 0;
            string strValue = "";
            string InSeparator = "";
            InSeparator = str.Substring(str.Length - separator.Length, separator.Length);
            if (InSeparator != separator)
            {
                str += separator;
            }
            Count = (str.Length - str.Replace(separator, "").Length) / separator.Length;
            string[] returnValue = new string[Count];
            for (int i = 0; i < Count; i++)
            {
                BitCount = str.IndexOf(separator, 0);
                strValue = str.Substring(0, BitCount);
                str = str.Replace(strValue + separator, "");
                //MessageBox.Show(BitCount.ToString() + ";" + strValue.ToString() + ";" + str);
                returnValue[i] = strValue;

            }
            return returnValue;
        }
        /// <summary>
        /// 取得分割后的字符串数据,数组以0开始,以默认的字符串分割
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>返回分割后的字符串数组</returns>
        /// <example>string str=Split("rrrrddrew@@tqwewerewddccc@@")[1].ToString()</example>
        public static string[] Split(string str)
        {
            int Len = str.Length;
            int Count = 0;
            int BitCount = 0;
            string strValue = "";
            string InSeparator = "";
            InSeparator = str.Substring(str.Length - SEPARATORSTRING.Length, SEPARATORSTRING.Length);
            if (InSeparator != SEPARATORSTRING)
            {
                str += SEPARATORSTRING;
            }
            Count = (str.Length - str.Replace(SEPARATORSTRING, "").Length) / SEPARATORSTRING.Length;
            string[] returnValue = new string[Count];
            for (int i = 0; i < Count; i++)
            {
                BitCount = str.IndexOf(SEPARATORSTRING, 0);
                strValue = str.Substring(0, BitCount);
                str = str.Replace(strValue + SEPARATORSTRING, "");
                //MessageBox.Show(BitCount.ToString() + ";" + strValue.ToString() + ";" + str);
                returnValue[i] = strValue;

            }
            return returnValue;
        } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值