Category

마메의여행기 (252)
사진 (84)
여행 (11)
등산 (30)
정보 (91)
개발 (36)

Search

Blog Menu

[VB6→VB.NET] 문자열 변환

1. 현재 날짜
[VB6]
TextBox1.Text = Format(Now, "yyyy/mm/dd")

[VB.NET] 자동변환
TextBox1.Text = String.Format(Now, "yyyy/mm/dd")
문제점 → 시분초까지 표시됨

[VB.NET] 수정
TextBox1.Text = Now.ToString("yyyy/MM/dd")
주의 → MM을 mm으로 표시하면 그대로 분이 출력됨

2. LeftB, MidB, RightB
선언
 Public Class VBStrings

        Public Shared Function LeftB(ByVal stTarget As String, ByVal iByteSize As Integer) As String
            Return MidB(stTarget, 1, iByteSize)
        End Function

        Public Shared Function MidB(ByVal stTarget As String, ByVal iStart As Integer) As String
            Dim hEncoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("Shift_JIS")
            Dim btBytes As Byte() = hEncoding.GetBytes(stTarget)
            Return hEncoding.GetString(btBytes, iStart - 1, btBytes.Length - iStart + 1)
        End Function

        Public Shared Function MidB _
        (ByVal stTarget As String, ByVal iStart As Integer, ByVal iByteSize As Integer) As String
            Dim hEncoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("Shift_JIS")
            Dim btBytes As Byte() = hEncoding.GetBytes(stTarget)
            Return hEncoding.GetString(btBytes, iStart - 1, iByteSize)
        End Function

        Public Shared Function RightB(ByVal stTarget As String, ByVal iByteSize As Integer) As String
            Dim hEncoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("Shift_JIS")
            Dim btBytes As Byte() = hEncoding.GetBytes(stTarget)
            Return hEncoding.GetString(btBytes, btBytes.Length - iByteSize, iByteSize)
        End Function

    End Class

사용예
    Dim stTarget As String = "ABCDEF"

    MessageBox.Show(VBStrings.LeftB(stTarget, 6))        'ABCD

    MessageBox.Show(VBStrings.MidB(stTarget, 4))         'CDEF

    MessageBox.Show(VBStrings.MidB(stTarget, 4, 5))      'CDE

    MessageBox.Show(VBStrings.RightB(stTarget, 3))       'EF

3. LenB
선언
Public Class VBStrings

    Public Shared Function LenB(ByVal stTarget As String) As Integer
        Return System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(stTarget)
    End Function

End
Class

사용예
    MessageBox.Show(VBStrings.LenB("あいうabc"))  '9