「VB.NET」文字列を半角から全角へ変換するサンプル

構文
StrConv(変換文字列, VbStrConv.Wide)
Public Enum VbStrConv
フィールド Wide 文字列内の横幅の狭い (半角) 文字を、横幅の広い (全角) 文字に変換します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Module Module1
Sub Main()
Dim strZen As String = "テスト123"
Console.WriteLine("半角文字: " + strZen)
Dim btCnt As Integer = System.Text.Encoding.GetEncoding(932).GetByteCount(strZen)
Console.WriteLine("半角文字のバイト数: " + btCnt.ToString())
'FunctiontestFuncを呼び出す
testFunc(strZen)
Console.ReadKey()
End Sub
'文字列を半角から全角へ変換
Public Function testFunc(ByVal target As String) As String
Dim res As String
'半角を全角へ変換
res = StrConv(target, VbStrConv.Wide)
Console.WriteLine("全角文字:" + res)
Dim btCnt As Integer = System.Text.Encoding.GetEncoding(932).GetByteCount(res)
Console.WriteLine("全角文字のバイト数:" + btCnt.ToString())
Return res
End Function
End Module
Module Module1 Sub Main() Dim strZen As String = "テスト123" Console.WriteLine("半角文字: " + strZen) Dim btCnt As Integer = System.Text.Encoding.GetEncoding(932).GetByteCount(strZen) Console.WriteLine("半角文字のバイト数: " + btCnt.ToString()) 'FunctiontestFuncを呼び出す testFunc(strZen) Console.ReadKey() End Sub '文字列を半角から全角へ変換 Public Function testFunc(ByVal target As String) As String Dim res As String '半角を全角へ変換 res = StrConv(target, VbStrConv.Wide) Console.WriteLine("全角文字:" + res) Dim btCnt As Integer = System.Text.Encoding.GetEncoding(932).GetByteCount(res) Console.WriteLine("全角文字のバイト数:" + btCnt.ToString()) Return res End Function End Module
Module Module1

    Sub Main()

        Dim strZen As String = "テスト123"
        Console.WriteLine("半角文字: " + strZen)

        Dim btCnt As Integer = System.Text.Encoding.GetEncoding(932).GetByteCount(strZen)
        Console.WriteLine("半角文字のバイト数: " + btCnt.ToString())

        'FunctiontestFuncを呼び出す
        testFunc(strZen)

        Console.ReadKey()
    End Sub

    '文字列を半角から全角へ変換
    Public Function testFunc(ByVal target As String) As String

        Dim res As String

        '半角を全角へ変換
        res = StrConv(target, VbStrConv.Wide)
        Console.WriteLine("全角文字:" + res)

        Dim btCnt As Integer = System.Text.Encoding.GetEncoding(932).GetByteCount(res)
        Console.WriteLine("全角文字のバイト数:" + btCnt.ToString())

        Return res

    End Function

End Module

実行結果
半角文字: テスト123
半角文字のバイト数: 6
全角文字:テスト123
全角文字のバイト数:12

VB.net

Posted by arkgame