How to declare a fixed-length string in VB.NET?

前端 未结 10 3019
醉酒成梦
醉酒成梦 2020-12-10 04:27

How do i Declare a string like this:

Dim strBuff As String * 256

in VB.NET?

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-10 04:57

    It depends on what you intend to use the string for. If you are using it for file input and output, you might want to use a byte array to avoid encoding problems. In vb.net, A 256-character string may be more than 256 bytes.

    Dim strBuff(256) as byte
    

    You can use encoding to transfer from bytes to a string

    Dim s As String
    Dim b(256) As Byte
    Dim enc As New System.Text.UTF8Encoding
    ...
    s = enc.GetString(b)
    

    You can assign 256 single-byte characters to a string if you need to use it to receive data, but the parameter passing may be different in vb.net than vb6.

    s = New String(" ", 256)
    

    Also, you can use vbFixedString. I'm not sure exactly what this does, however, because when you assign a string of different length to a variable declared this way, it becomes the new length.

     Public s As String
    s = "1234567890" ' len(s) is now 10
    

提交回复
热议问题