日期:2010-07-20  浏览次数:20448 次

Public Class uStringClass uString
    Private Shared gEncoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("GB2312")

    Public Shared Property Encoding()Property Encoding() As System.Text.Encoding
        Get
            Return gEncoding
        End Get
        Set(ByVal value As System.Text.Encoding)
            gEncoding = value
        End Set
    End Property

    Public Shared Function Length()Function Length(ByVal s As String) As Integer
        Return gEncoding.GetByteCount(s)
    End Function

    Public Shared Function PadLeft()Function PadLeft(ByVal s As String, ByVal totalWidth As Integer, ByVal paddingChar As Char) As String
        Return s.PadLeft(totalWidth + s.Length - Length(s), paddingChar)
    End Function

    Public Shared Function PadLeft()Function PadLeft(ByVal s As String, ByVal totalWidth As Integer) As String
        Return s.PadLeft(totalWidth + s.Length - Length(s))
    End Function

    Public Shared Function PadRight()Function PadRight(ByVal s As String, ByVal totalWidth As Integer, ByVal paddingChar As Char) As String
        Return s.PadRight(totalWidth + s.Length - Length(s), paddingChar)
    End Function

    Public Shared Function PadRight()Function PadRight(ByVal s As String, ByVal totalWidth As Integer) As String
        Return s.PadRight(totalWidth + s.Length - Length(s))
    End Function

    Public Shared Function SubString()Function SubString(ByVal Line As String, ByVal Index As Integer, ByVal Length As Integer) As String
        Return gEncoding.GetString(gEncoding.GetBytes(Line), Index, Length)
    End Function

End Class

测试对比:

    Private Sub ToolStripButton6_Click()Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
        Dim tmp As String = "中国China"
        Console.WriteLine(LzmTW.uString.Encoding.EncodingName)


        Console.WriteLine(LzmTW.uString.Length(tmp))
        Console.WriteLine(tmp.Length)

        Console.WriteLine(tmp.Substring(0, 2))
        Console.WriteLine(LzmTW.uString.SubString(tmp, 0, 2))


        Console.WriteLine(New String("*"c, 20))

        Console.WriteLine(LzmTW.uString.PadLeft(tmp, 20, "*"c))
        Console.WriteLine(tmp.PadLeft(20, "*"c))

        Console.WriteLine(LzmTW.uString.PadRight(tmp, 20, "*"c))
        Console.WriteLine(tmp.PadRight(20, "*"c))

        Console.WriteLine(LzmTW.uString.PadLeft(tmp, 20))
        Console.WriteLine(tmp.PadLeft(20))

        Console.WriteLine(LzmTW.uString.PadRight(tmp, 20))
    &nbs