【VB.NET】漢字の文字数を取得する



1.文字列の文字数を取得する。(2方法)

Private Sub Button1_Click(ByVal sender As System.Object, _
                          
ByVal e As System.EventArgs) Handles Button1.Click
''文字列の文字数を取得する
    
Dim str1 As String = "123あいう"

    'String.Length プロパティ を使っての文字数を取得
    Debug.WriteLine(str1.Length)    
'結果  6

    'Len 関数を使っての取得
    Debug.WriteLine(Microsoft.VisualBasic.Len(str1))    
'結果  6
End Sub

2.半角2バイト換算での文字列のバイト数を取得する。

Private Sub Button2_Click(ByVal sender As System.Object, _
                          
ByVal e As System.EventArgs) Handles Button2.Click
''半角2バイト換算での文字列のバイト数を取得する。
    
Dim str1 As String = "123あいう"
    
Dim LenB As Integer
    'Encoding.GetByteCount メソッド
    '指定した文字配列をエンコードするために必要なバイト数を計算します
    LenB = System.Text.Encoding.
Unicode.GetByteCount(str1)
    Debug.WriteLine(LenB.ToString)     
'結果  12

    'VB.NET には LenB 関数はありません。
End Sub

3.半角1バイト・全角2バイト換算での文字列のバイト数を取得する。

Private Sub Button3_Click(ByVal sender As System.Object, _
                          
ByVal e As System.EventArgs) Handles Button3.Click
''半角1バイト換算での文字列のバイト数を取得する。
    
Dim str1 As String = "123あいう"
    
Dim LenB As Integer
    
Dim s_jis As System.Text.Encoding = System.Text.Encoding.GetEncoding("Shift_JIS")
    LenB = s_jis.GetByteCount(str1)
    Debug.WriteLine(LenB.ToString)      
'結果  9

    '又は
    LenB = System.Text.Encoding.GetEncoding("shift-jis").GetByteCount(str1)
    Debug.WriteLine(LenB.ToString)      
'結果  9

    LenB = System.Text.Encoding.GetEncoding(932).GetByteCount(str1)
    Debug.WriteLine(LenB.ToString)      
'結果  9

    '参考  システムの現在の ANSI コード ページのエンコーディングに必要なバイト数
    Debug.WriteLine(System.Text.Encoding.Default.GetByteCount(str1))    
'結果  9
End Sub


1 nhận xét:

  1. The History of the Casino - One of the Most Popular Casinos
    A relative casinosites.one newcomer to the world of online gambling, 토토 사이트 Wynn Las Vegas opened its https://septcasino.com/review/merit-casino/ doors to a https://deccasino.com/review/merit-casino/ new https://vannienailor4166blog.blogspot.com/ audience of over 600,000 in 2017. This was the first casino

    Trả lờiXóa

Được tạo bởi Blogger.