VB.NET
Public Shared Function CharByte(ByVal vstrWord As String) As Integer
'aStr字串之第一個字的位元組長度
If Len(vstrWord) = 0 Then
Return 0
Else
Select Case Asc(vstrWord)
Case 0 To 255
Return 1
Case Else
Return 2
End Select
End If
End Function
Public Shared Function StrLenB(ByVal vstrValue As String) As Integer
'如同VB 6.0的LenB函數,傳回字串aStr的位元組長度
Dim i, k As Integer
For i = 1 To Len(vstrValue)
k += CharByte(Mid(vstrValue, i, 1))
Next
Return k
End Function
MessageBox.Show(StrLenB("a").ToString()) 'return 1
MessageBox.Show(StrLenB("abc").ToString()) 'return 3
MessageBox.Show(StrLenB("a亂").ToString()) 'return 3
MessageBox.Show(StrLenB("a亂b").ToString()) 'return 4
C#public static int CharByte(string vstrWord){//aStr字串之第一個字的位元組長度if (vstrWord.Length == 0){return 0;}else{if(Convert.ToInt32(vstrWord[0]) > 255){return 2;}else{return 1;}}}public static int StrLenB(string vstrValue){//如同VB 6.0的LenB函數,傳回字串aStr的位元組長度int i = 0;int k = 0;for (i =0; i < vstrValue.Length ; i++){k += CharByte(vstrValue.Substring( i, 1));}return k;}MessageBox.Show(StrLenB("a").ToString()); //return 1MessageBox.Show(StrLenB("abc").ToString()); //return 3MessageBox.Show(StrLenB("a亂").ToString()); //return 3MessageBox.Show(StrLenB("a亂b").ToString()); //return 4
沒有留言:
張貼留言