티스토리 뷰

매번 string 형식을 byte로 잘라서 써야 될때가 많은데 그럴때마다 만들어서 썻던거 같다.
이렇게 정의 해놓고 쓴다면 훨씬 편한듯 하네요.

ex)아래 형식을 확인해 보면 값이 = Strin..으로 나오게 됩니다.

string str_string = StringCutByte("String이다.", 10, ".."); 


protected string StringCutByte(string str, int i_ByteCount, string str_attach)

    {

        if (i_ByteCount< 3)

            throw new ArgumentOutOfRangeException("i_ByteCount");

        StringBuilder sb = new StringBuilder(i_ByteCount);

        foreach (char c in str)

        {

            sb.Append(c);

            int count = System.Text.Encoding.Default.GetByteCount(sb.ToString());

            if (count > i_ByteCount)

            {

                sb.Remove(sb.Length - 1, 1);

                while (System.Text.Encoding.Default.GetByteCount(sb.ToString() ) > i_ByteCount&& sb.Length > 0)

                {

                    sb.Remove(sb.Length - 1, 1);

                }

                sb.Append(str_attach);

                break;

            }

        }

        return sb.ToString();

    }

댓글
최근에 올라온 글
최근에 달린 댓글
글 보관함
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Total
Today
Yesterday