2010年12月8日 星期三

Vb.Net 中英混合 Substring

'Imports System
'Imports System.Text

Public Function Substring_ByByte_Big5(ByVal inString As String, ByVal StartByte As Integer, Optional ByVal Length As Integer = 0) As String

'BIG5:中文2碼英文1碼
Dim byte_Big5() As Byte = Encoding.GetEncoding("BIG5").GetBytes(inString)
Dim intTotLength As Integer = byte_Big5.Length
Dim intRtnLength As Integer
Dim strRtn As String = ""

If Length = 0 Then
intRtnLength = intTotLength - StartByte
Else
If Length + StartByte > intTotLength Then
intRtnLength = intTotLength - StartByte
Else
intRtnLength = Length
End If
End If

Dim byteRtn(intRtnLength - 1) As Byte
Dim intPOS As Integer

For intPOS = 0 To intRtnLength - 1
byteRtn(intPOS) = byte_Big5(StartByte + intPOS)
Next

strRtn = Encoding.GetEncoding("BIG5").GetString(byteRtn)

Return strRtn

End Function

'執行結果
'strS = "中英混合CHINESE-ENGLISH-MIX"

'Substring_ByByte_Big5(strS, 0) = 中英混合CHINESE-ENGLISH-MIX
'Substring_ByByte_Big5(strS, 1) = 五^混合CHINESE-ENGLISH-MIX
'Substring_ByByte_Big5(strS, 2) = 英混合CHINESE-ENGLISH-MIX
'Substring_ByByte_Big5(strS, 3) = ^混合CHINESE-ENGLISH-MIX

'Substring_ByByte_Big5(strS, 0, 12) = 中英混合CHIN
'Substring_ByByte_Big5(strS, 1, 12) = 五^混合CHINE
'Substring_ByByte_Big5(strS, 2, 12) = 英混合CHINES
'Substring_ByByte_Big5(strS, 3, 12) = ^混合CHINESE

沒有留言: