如何在 VB.NET 中声明一个固定长度的字符串? [英] How to declare a fixed-length string in VB.NET?

查看:67
本文介绍了如何在 VB.NET 中声明一个固定长度的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何声明这样的字符串:

How do i Declare a string like this:

Dim strBuff As String * 256

在 VB.NET 中?

in VB.NET?

推荐答案

这取决于您打算将字符串用于什么目的.如果将其用于文件输入和输出,则可能需要使用字节数组来避免编码问题.在 vb.net 中,一个 256 个字符的字符串可能超过 256 个字节.

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)

如果需要使用字符串接收数据,可以给字符串分配256个单字节字符,但vb.net中的参数传递可能与vb6不同.

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)

此外,您可以使用 vbFixedString.但是,我不确定这到底是做什么的,因为当您将不同长度的字符串分配给以这种方式声明的变量时,它会成为新的长度.

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.

<VBFixedString(6)> Public s As String
s = "1234567890" ' len(s) is now 10

这篇关于如何在 VB.NET 中声明一个固定长度的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆