如何在VB.NET中声明固定长度的字符串?

时间:2022-04-24 02:12:03

How do i Declare a string like this:

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

Dim strBuff As String * 256

in VB.NET?

在VB.NET ?

10 个解决方案

#1


6  

Use the VBFixedString attribute. See the MSDN info here

使用VBFixedString属性。在这里查看MSDN信息。

<VBFixedString(256)>Dim strBuff As String

#2


6  

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.

这取决于您想要使用字符串的目的。如果您将其用于文件输入和输出,您可能希望使用字节数组来避免编码问题。在vb.net中,256字符的字符串可能超过256字节。

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.

如果需要使用它来接收数据,可以将256个单字节字符分配给一个字符串,但是在vb.net中传递的参数可能不同于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.

此外,还可以使用vbFixedString。但是,我不确定这到底是怎么回事,因为当您为一个声明的变量指定一个不同长度的字符串时,它就变成了新的长度。

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

#3


3  

To write this VB 6 code:

要编写这个VB 6代码:

Dim strBuff As String * 256

In VB.Net you can use something like:

在VB。你可以使用类似的东西:

Dim strBuff(256) As Char

#4


2  

Use stringbuilder

使用stringbuilder

'Declaration   
Dim S As New System.Text.StringBuilder(256, 256)
'Adding text
S.append("abc")
'Reading text
S.tostring

#5


1  

You can use Microsoft.VisualBasic.Compatibility:

您可以使用Microsoft.VisualBasic.Compatibility:

Imports Microsoft.VisualBasic.Compatibility

Dim strBuff As New VB6.FixedLengthString(256)

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.

但是它被标记为过时的,特别是不支持64位进程,所以编写您自己的复制功能,这是为了在设置长值和空格时截断短值。它还设置了一个“未初始化”的值,如上面所示,为null。

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):

来自LinqPad的示例代码(我不能允许导入Microsoft.VisualBasic)。我认为兼容性是过时的,但我没有证据证明这一点):

Imports Microsoft.VisualBasic.Compatibility

Dim U As New VB6.FixedLengthString(5)
Dim S As New VB6.FixedLengthString(5, "Test")
Dim L As New VB6.FixedLengthString(5, "Testing")
Dim p0 As Func(Of String, String) = Function(st) """" & st.Replace(ChrW(0), "\0") & """"
p0(U.Value).Dump()
p0(S.Value).Dump()
p0(L.Value).Dump()
U.Value = "Test"
p0(U.Value).Dump()
U.Value = "Testing"
p0(U.Value).Dump()

which has this output:

有这个输出:

"\0\0\0\0\0"
"Test "
"Testi"
"Test "
"Testi"

"\0\0\0\0\0 \0" "测试" "测试" "测试"

#6


1  

This object can be defined as a structure with one constructor and two properties.

该对象可以定义为具有一个构造函数和两个属性的结构。

Public Structure FixedLengthString
     Dim mValue As String
     Dim mSize As Short

     Public Sub New(Size As Integer)
         mSize = Size
         mValue = New String(" ", mSize)
     End Sub

     Public Property Value As String
         Get
             Value = mValue
         End Get

         Set(value As String)
             If value.Length < mSize Then
                 mValue = value & New String(" ", mSize - value.Length)
             Else
                 mValue = value.Substring(0, mSize)
             End If
         End Set
     End Property
 End Structure

https://jdiazo.wordpress.com/2012/01/12/getting-rid-of-vb6-compatibility-references/

https://jdiazo.wordpress.com/2012/01/12/getting-rid-of-vb6-compatibility-references/

#7


0  

Try this:

试试这个:

    Dim strbuf As New String("A", 80)

Creates a 80 character string filled with "AAA...."'s

创建一个包含“AAA”的80个字符串。

Here I read a 80 character string from a binary file:

在这里,我从二进制文件读取了一个80字符的字符串:

    FileGet(1,strbuf)

reads 80 characters into strbuf...

将80个字符读入strbuf…

#8


-1  

Have you tried

你有试过

Dim strBuff as String

Also see Working with Strings in .NET using VB.NET

还可以使用VB.NET在.NET中使用字符串。

This tutorial explains how to represent strings in .NET using VB.NET and how to work with them with the help of .NET class library classes.

本教程将介绍如何在. net中使用VB来表示字符串。NET以及如何在.NET类库类的帮助下与它们一起工作。

#9


-1  

Dim a as string

a = ...

If a.length > theLength then

     a = Mid(a, 1, theLength)

End If

#10


-1  

This hasn't been fully tested, but here's a class to solve this problem:

这还没有经过充分的测试,但是这里有一个类来解决这个问题:

''' <summary>
''' Represents a <see cref="String" /> with a minimum
''' and maximum length.
''' </summary>
Public Class BoundedString

    Private mstrValue As String

    ''' <summary>
    ''' The contents of this <see cref="BoundedString" />
    ''' </summary>
    Public Property Value() As String
        Get
            Return mstrValue
        End Get

        Set(value As String)
            If value.Length < MinLength Then
                Throw New ArgumentException(String.Format("Provided string {0} of length {1} contains less " &
                                                          "characters than the minimum allowed length {2}.",
                                                          value, value.Length, MinLength))
            End If

            If value.Length > MaxLength Then
                Throw New ArgumentException(String.Format("Provided string {0} of length {1} contains more " &
                                                          "characters than the maximum allowed length {2}.",
                                                          value, value.Length, MaxLength))
            End If

            If Not AllowNull AndAlso value Is Nothing Then
                Throw New ArgumentNullException(String.Format("Provided string {0} is null, and null values " &
                                                              "are not allowed.", value))
            End If

            mstrValue = value
        End Set
    End Property

    Private mintMinLength As Integer
    ''' <summary>
    ''' The minimum number of characters in this <see cref="BoundedString" />.
    ''' </summary>
    Public Property MinLength() As Integer
        Get
            Return mintMinLength
        End Get

        Private Set(value As Integer)
            mintMinLength = value
        End Set

    End Property

    Private mintMaxLength As Integer
    ''' <summary>
    ''' The maximum number of characters in this <see cref="BoundedString" />.
    ''' </summary>
    Public Property MaxLength As Integer
        Get
            Return mintMaxLength
        End Get

        Private Set(value As Integer)
            mintMaxLength = value
        End Set
    End Property

    Private mblnAllowNull As Boolean
    ''' <summary>
    ''' Whether or not this <see cref="BoundedString" /> can represent a null value.
    ''' </summary>
    Public Property AllowNull As Boolean
        Get
            Return mblnAllowNull
        End Get

        Private Set(value As Boolean)
            mblnAllowNull = value
        End Set
    End Property

    Public Sub New(ByVal strValue As String,
                   ByVal intMaxLength As Integer)
        MinLength = 0
        MaxLength = intMaxLength
        AllowNull = False

        Value = strValue
    End Sub

    Public Sub New(ByVal strValue As String,
                   ByVal intMinLength As Integer,
                   ByVal intMaxLength As Integer)
        MinLength = intMinLength
        MaxLength = intMaxLength
        AllowNull = False

        Value = strValue
    End Sub

    Public Sub New(ByVal strValue As String,
                   ByVal intMinLength As Integer,
                   ByVal intMaxLength As Integer,
                   ByVal blnAllowNull As Boolean)
        MinLength = intMinLength
        MaxLength = intMaxLength
        AllowNull = blnAllowNull

        Value = strValue
    End Sub
End Class

#1


6  

Use the VBFixedString attribute. See the MSDN info here

使用VBFixedString属性。在这里查看MSDN信息。

<VBFixedString(256)>Dim strBuff As String

#2


6  

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.

这取决于您想要使用字符串的目的。如果您将其用于文件输入和输出,您可能希望使用字节数组来避免编码问题。在vb.net中,256字符的字符串可能超过256字节。

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.

如果需要使用它来接收数据,可以将256个单字节字符分配给一个字符串,但是在vb.net中传递的参数可能不同于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.

此外,还可以使用vbFixedString。但是,我不确定这到底是怎么回事,因为当您为一个声明的变量指定一个不同长度的字符串时,它就变成了新的长度。

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

#3


3  

To write this VB 6 code:

要编写这个VB 6代码:

Dim strBuff As String * 256

In VB.Net you can use something like:

在VB。你可以使用类似的东西:

Dim strBuff(256) As Char

#4


2  

Use stringbuilder

使用stringbuilder

'Declaration   
Dim S As New System.Text.StringBuilder(256, 256)
'Adding text
S.append("abc")
'Reading text
S.tostring

#5


1  

You can use Microsoft.VisualBasic.Compatibility:

您可以使用Microsoft.VisualBasic.Compatibility:

Imports Microsoft.VisualBasic.Compatibility

Dim strBuff As New VB6.FixedLengthString(256)

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.

但是它被标记为过时的,特别是不支持64位进程,所以编写您自己的复制功能,这是为了在设置长值和空格时截断短值。它还设置了一个“未初始化”的值,如上面所示,为null。

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):

来自LinqPad的示例代码(我不能允许导入Microsoft.VisualBasic)。我认为兼容性是过时的,但我没有证据证明这一点):

Imports Microsoft.VisualBasic.Compatibility

Dim U As New VB6.FixedLengthString(5)
Dim S As New VB6.FixedLengthString(5, "Test")
Dim L As New VB6.FixedLengthString(5, "Testing")
Dim p0 As Func(Of String, String) = Function(st) """" & st.Replace(ChrW(0), "\0") & """"
p0(U.Value).Dump()
p0(S.Value).Dump()
p0(L.Value).Dump()
U.Value = "Test"
p0(U.Value).Dump()
U.Value = "Testing"
p0(U.Value).Dump()

which has this output:

有这个输出:

"\0\0\0\0\0"
"Test "
"Testi"
"Test "
"Testi"

"\0\0\0\0\0 \0" "测试" "测试" "测试"

#6


1  

This object can be defined as a structure with one constructor and two properties.

该对象可以定义为具有一个构造函数和两个属性的结构。

Public Structure FixedLengthString
     Dim mValue As String
     Dim mSize As Short

     Public Sub New(Size As Integer)
         mSize = Size
         mValue = New String(" ", mSize)
     End Sub

     Public Property Value As String
         Get
             Value = mValue
         End Get

         Set(value As String)
             If value.Length < mSize Then
                 mValue = value & New String(" ", mSize - value.Length)
             Else
                 mValue = value.Substring(0, mSize)
             End If
         End Set
     End Property
 End Structure

https://jdiazo.wordpress.com/2012/01/12/getting-rid-of-vb6-compatibility-references/

https://jdiazo.wordpress.com/2012/01/12/getting-rid-of-vb6-compatibility-references/

#7


0  

Try this:

试试这个:

    Dim strbuf As New String("A", 80)

Creates a 80 character string filled with "AAA...."'s

创建一个包含“AAA”的80个字符串。

Here I read a 80 character string from a binary file:

在这里,我从二进制文件读取了一个80字符的字符串:

    FileGet(1,strbuf)

reads 80 characters into strbuf...

将80个字符读入strbuf…

#8


-1  

Have you tried

你有试过

Dim strBuff as String

Also see Working with Strings in .NET using VB.NET

还可以使用VB.NET在.NET中使用字符串。

This tutorial explains how to represent strings in .NET using VB.NET and how to work with them with the help of .NET class library classes.

本教程将介绍如何在. net中使用VB来表示字符串。NET以及如何在.NET类库类的帮助下与它们一起工作。

#9


-1  

Dim a as string

a = ...

If a.length > theLength then

     a = Mid(a, 1, theLength)

End If

#10


-1  

This hasn't been fully tested, but here's a class to solve this problem:

这还没有经过充分的测试,但是这里有一个类来解决这个问题:

''' <summary>
''' Represents a <see cref="String" /> with a minimum
''' and maximum length.
''' </summary>
Public Class BoundedString

    Private mstrValue As String

    ''' <summary>
    ''' The contents of this <see cref="BoundedString" />
    ''' </summary>
    Public Property Value() As String
        Get
            Return mstrValue
        End Get

        Set(value As String)
            If value.Length < MinLength Then
                Throw New ArgumentException(String.Format("Provided string {0} of length {1} contains less " &
                                                          "characters than the minimum allowed length {2}.",
                                                          value, value.Length, MinLength))
            End If

            If value.Length > MaxLength Then
                Throw New ArgumentException(String.Format("Provided string {0} of length {1} contains more " &
                                                          "characters than the maximum allowed length {2}.",
                                                          value, value.Length, MaxLength))
            End If

            If Not AllowNull AndAlso value Is Nothing Then
                Throw New ArgumentNullException(String.Format("Provided string {0} is null, and null values " &
                                                              "are not allowed.", value))
            End If

            mstrValue = value
        End Set
    End Property

    Private mintMinLength As Integer
    ''' <summary>
    ''' The minimum number of characters in this <see cref="BoundedString" />.
    ''' </summary>
    Public Property MinLength() As Integer
        Get
            Return mintMinLength
        End Get

        Private Set(value As Integer)
            mintMinLength = value
        End Set

    End Property

    Private mintMaxLength As Integer
    ''' <summary>
    ''' The maximum number of characters in this <see cref="BoundedString" />.
    ''' </summary>
    Public Property MaxLength As Integer
        Get
            Return mintMaxLength
        End Get

        Private Set(value As Integer)
            mintMaxLength = value
        End Set
    End Property

    Private mblnAllowNull As Boolean
    ''' <summary>
    ''' Whether or not this <see cref="BoundedString" /> can represent a null value.
    ''' </summary>
    Public Property AllowNull As Boolean
        Get
            Return mblnAllowNull
        End Get

        Private Set(value As Boolean)
            mblnAllowNull = value
        End Set
    End Property

    Public Sub New(ByVal strValue As String,
                   ByVal intMaxLength As Integer)
        MinLength = 0
        MaxLength = intMaxLength
        AllowNull = False

        Value = strValue
    End Sub

    Public Sub New(ByVal strValue As String,
                   ByVal intMinLength As Integer,
                   ByVal intMaxLength As Integer)
        MinLength = intMinLength
        MaxLength = intMaxLength
        AllowNull = False

        Value = strValue
    End Sub

    Public Sub New(ByVal strValue As String,
                   ByVal intMinLength As Integer,
                   ByVal intMaxLength As Integer,
                   ByVal blnAllowNull As Boolean)
        MinLength = intMinLength
        MaxLength = intMaxLength
        AllowNull = blnAllowNull

        Value = strValue
    End Sub
End Class