切割字串中英文夾雜

切割字串中英文夾雜

在DotNet

中文字長度為1轉成文字檔時長度用byte計算時會發生錯誤

參考了下列文章

中英文夾雜且有全形及半形混雜的標點符號,該如何正確的換行及斷字

改寫程式

 

        /// <summary>
        /// Splits the string.
        /// </summary>
        /// <param name="strData">The STR data.</param>
        /// <param name="Format">The format.</param>
        /// <returns></returns>
        public static List<string> SplitString(string strData, int[] Format)
        {
            string strResult = "";
            List<string> result = new List<string>();
            int IndexCount = 0;
            int intLoop;
            int intPos = 0;
            int intNext;
            byte[] byt = Encoding.Default.GetBytes(strData);
            for (intLoop = 0; intLoop <= byt.Length - 1; intLoop++)
            {
                if (byt[intLoop] >= 128)
                {
                    intNext = 2;
                }
                else
                {
                    intNext = 1;
                }
                strResult += Encoding.Default.GetString(byt, intLoop, intNext);
                if (intNext == 2)
                {
                    intLoop += 1;
                }
                intPos += intNext;
                if (intPos >= int.Parse(Format[IndexCount].ToString()))
                {
                    result.Add(strResult);
                    strResult = "";
                    intPos = 0;
                    IndexCount++;

                }
            }
            return result;
        }

用法如下

 

protected void Button1_Click(object sender, EventArgs e)
    {
        string test = "你好ab我";
        int[] ColumnLength = new int[] { 4, 1, 1, 2 };
        List<string> result = StringUtility.SplitString(test, ColumnLength);
        //result[0]=你好,result[1]=a,result[2]=b,result[3]=我

    }

再更進一步改寫

 

        /// <summary>
        /// Subs the string.
        /// </summary>
        /// <param name="strData">The STR data.</param>
        /// <param name="From">From.</param>
        /// <param name="Length">The length.</param>
        /// <returns></returns>
        public static string SubString(string strData, int From, int Length , string OverFlowPadChar , bool IsLeftSide)
        {
            if (strData != string.Empty)
            {
                int ResultLength = Length;

                string PadBlank = "";
                byte[] byt = Encoding.Default.GetBytes(strData);
                if (Length > byt.Length)
                {
                    ResultLength = byt.Length;
                    for (int i = 0; i < Length - byt.Length; i++)
                        PadBlank += OverFlowPadChar;
                    for (int j = 0; j < From; j++)
                        PadBlank += OverFlowPadChar;
                }
                int RestLength = byt.Length - ResultLength;
                int[] Size;
                if (From != 0)
                {
                    if (RestLength > 0)
                        Size = new int[] { From, ResultLength, RestLength };
                    else
                        Size = new int[] { From, ResultLength - From };
                }
                else
                {
                    if (RestLength > 0)
                        Size = new int[] { ResultLength, RestLength };
                    else
                        Size = new int[] { ResultLength };
                }
                List<string> a = SplitString(strData, Size);
                if (From != 0)
                {
                    if (IsLeftSide)
                        return PadBlank + a[1].ToString();
                    else
                        return a[1].ToString() + PadBlank;
                }
                else
                {
                    if (IsLeftSide)
                        return PadBlank + a[0].ToString();
                    else
                        return a[0].ToString() + PadBlank;
                }
            }
            else
            {
                string PadBlank = string.Empty;
                for (int i = 0; i < Length; i++)
                    PadBlank += OverFlowPadChar;
                return PadBlank;
            }
        }

長度是用byte計算,所以中文字算2

用法如下

protected void Button1_Click(object sender, EventArgs e)
    {
        string result1 = StringUtility.SubString("test", 0, 6, "0", true);
        // result1 = "00test";
        string result2 = StringUtility.SubString("test", 0, 6, "0", false);
        // result2 = "test00";
        string result3 = StringUtility.SubString("test", 2, 6, "0", true);
        // result3 = "0000st";
        string result4 = StringUtility.SubString("test", 2, 6, "0", false);
        // result4 = "st0000";

    }