如何在VB.NET中声明固定长度的字符串?
声明这样的字符串:
Dim strBuff As String * 256
如何在 VB.NET 中
How do i Declare a string like this:
Dim strBuff As String * 256
in VB.NET?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
使用 VBFixedString 属性。请参阅此处的 MSDN 信息
Use the VBFixedString attribute. See the MSDN info here
这取决于您打算使用该字符串做什么。如果您将其用于文件输入和输出,您可能需要使用字节数组来避免编码问题。在vb.net中,256个字符的字符串可能超过256个字节。
可以使用编码从字节传输到字符串。
如果需要使用字符串接收数据,可以为字符串分配256个单字节字符,但vb.net中的参数传递可能与vb6不同。
另外,您可以使用 vbFixedString。然而,我不确定这到底是做什么的,因为当您将不同长度的字符串分配给以这种方式声明的变量时,它就会变成新的长度。
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.
You can use encoding to transfer from bytes to a string
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.
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.
要编写此 VB 6 代码:
在 VB.Net 中,您可以使用以下内容:
To write this VB 6 code:
In VB.Net you can use something like:
使用字符串生成器
Use stringbuilder
试试这个:
创建一个 80 个字符的字符串,填充“AAA....”在
这里我从二进制文件中读取一个 80 个字符的字符串:
将 80 个字符读入 strbuf...
Try this:
Creates a 80 character string filled with "AAA...."'s
Here I read a 80 character string from a binary file:
reads 80 characters into strbuf...
您可以使用 Microsoft.VisualBasic.Compatibility :
但它被标记为 已过时,并且特别不支持 64 位进程,因此请编写您自己的来复制该功能,即在设置长值时截断并在短值时用空格填充。它还将“未初始化”值(如上所示)设置为空。
来自 LinqPad 的示例代码(我无法允许
Imports Microsoft.VisualBasic.Compatibility
我认为是因为它已标记为过时,但我没有证明):它具有以下输出:
You can use
Microsoft.VisualBasic.Compatibility
:But it's marked as obsolete and specifically not supported for 64-bit processes, so write your own that replicates the functionality, which is to truncate on setting long values and padding right with spaces for short values. It also sets an "uninitialised" value, like above, to nulls.
Sample code from LinqPad (which I can't get to allow
Imports Microsoft.VisualBasic.Compatibility
I think because it is marked obsolete, but I have no proof of that):which has this output:
该对象可以定义为具有一个构造函数和两个属性的结构。
https://jdiazo.wordpress.com /2012/01/12/摆脱vb6-compatibility-references/
This object can be defined as a structure with one constructor and two properties.
https://jdiazo.wordpress.com/2012/01/12/getting-rid-of-vb6-compatibility-references/
您是否尝试过
另请参阅使用 VB.NET 在 .NET 中处理字符串
Have you tried
Also see Working with Strings in .NET using VB.NET
这还没有经过充分测试,但这里有一个类来解决这个问题:
This hasn't been fully tested, but here's a class to solve this problem: