返回指定长度的字符(已字节为单位)
一、
Dim arrb() As Byte
Dim desStr As String
arrb = Encoding.Default.GetBytes(x)
desStr = Encoding.Default.GetString(arrb, 0, 255)
Dim desStr As String
arrb = Encoding.Default.GetBytes(x)
desStr = Encoding.Default.GetString(arrb, 0, 255)
二、
Public Class BLString
''' -----------------------------------------------------------------------------
''' <summary>
''' 取得字符串的字节数
''' </summary>
''' <param name="str">任意字符串</param>
''' <returns>传入字符串参数的字节数</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [Zhouyou] 2005-11-2 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Function LenByte(ByVal str As String) As Int32
Dim myByte() As Byte = System.Text.Encoding.Default.GetBytes(str)
Return myByte.Length
End Function
''' -----------------------------------------------------------------------------
''' <summary>
''' 从字符串左侧开始截取指定字节数子字符串
''' </summary>
''' <param name="str">任意字符串</param>
''' <param name="cutLen">截取字符串的字节长度</param>
''' <returns>子字符串</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [Zhouyou] 2005-11-2 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Function LeftByte(ByVal str As String, ByVal cutLen As Int32) As String
Dim myByte() As Byte = System.Text.Encoding.Default.GetBytes(str)
If myByte.Length > cutLen Then
Dim returnStr As String = ""
Dim i As Int32
For i = 0 To str.Length
Dim tempByte() As Byte = System.Text.Encoding.Default.GetBytes(returnStr)
If tempByte.Length < cutLen - 4 Then
returnStr &= str.Substring(i, 1)
Else
Exit For
End If
Next
Return returnStr
Else
Return str
End If
End Function
End Class
'编码字符,返回指定长度的字符
Function HtmlEncode(ByVal html As String, ByVal len As Int32) As String
If html = String.Empty Then Exit Function
len = len * 2
If getStrByts.LenByte(html) > len Then
html = getStrByts.LeftByte(html, len) & "."
End If
Return Server.HtmlEncode(html)
End Function
''' -----------------------------------------------------------------------------
''' <summary>
''' 取得字符串的字节数
''' </summary>
''' <param name="str">任意字符串</param>
''' <returns>传入字符串参数的字节数</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [Zhouyou] 2005-11-2 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Function LenByte(ByVal str As String) As Int32
Dim myByte() As Byte = System.Text.Encoding.Default.GetBytes(str)
Return myByte.Length
End Function
''' -----------------------------------------------------------------------------
''' <summary>
''' 从字符串左侧开始截取指定字节数子字符串
''' </summary>
''' <param name="str">任意字符串</param>
''' <param name="cutLen">截取字符串的字节长度</param>
''' <returns>子字符串</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [Zhouyou] 2005-11-2 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Function LeftByte(ByVal str As String, ByVal cutLen As Int32) As String
Dim myByte() As Byte = System.Text.Encoding.Default.GetBytes(str)
If myByte.Length > cutLen Then
Dim returnStr As String = ""
Dim i As Int32
For i = 0 To str.Length
Dim tempByte() As Byte = System.Text.Encoding.Default.GetBytes(returnStr)
If tempByte.Length < cutLen - 4 Then
returnStr &= str.Substring(i, 1)
Else
Exit For
End If
Next
Return returnStr
Else
Return str
End If
End Function
End Class
'编码字符,返回指定长度的字符
Function HtmlEncode(ByVal html As String, ByVal len As Int32) As String
If html = String.Empty Then Exit Function
len = len * 2
If getStrByts.LenByte(html) > len Then
html = getStrByts.LeftByte(html, len) & "."
End If
Return Server.HtmlEncode(html)
End Function