【.Net】含Unicode的字符串截断 VB.NET C#

 

 

    Function AnsiLeftB(ByVal strArg As String, ByVal arg1 As Integer) As String
        Dim unicodeEncoding As Encoding = Encoding.GetEncoding("Shift_JIS")
        Dim unicodeBytes() As Byte = unicodeEncoding.GetBytes(strArg)

        Dim tempLen As Integer = 0
        For i As Integer = 0 To strArg.Length
            Dim j As Integer = unicodeEncoding.GetByteCount(strArg(i))
            If tempLen + j <= arg1 Then
                tempLen = tempLen + j
            Else
                Exit For
            End If
        Next
        Dim tempBytes(tempLen) As Byte
        Array.Copy(unicodeBytes, tempBytes, tempLen)
        AnsiLeftB = unicodeEncoding.GetString(tempBytes)
    End Function
View Code

 

posted on 2016-03-15 11:28  tony_cyou  阅读(402)  评论(0编辑  收藏  举报

导航