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

时间:2010-02-21 08:56:18

标签: vb.net vb6-migration

如何声明这样的字符串:

Dim strBuff As String * 256
在VB.NET中

10 个答案:

答案 0 :(得分:6)

使用VBFixedString属性。请参阅MSDN信息here

<VBFixedString(256)>Dim strBuff As String

答案 1 :(得分:6)

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

Dim strBuff(256) as byte

您可以使用编码从字节转换为字符串

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

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

s = New String(" ", 256)

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

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

答案 2 :(得分:3)

编写这个VB 6代码:

Dim strBuff As String * 256

在VB.Net中,你可以使用类似的东西:

Dim strBuff(256) As Char

答案 3 :(得分:3)

使用stringbuilder

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

答案 4 :(得分:1)

您可以使用Microsoft.VisualBasic.Compatibility

Imports Microsoft.VisualBasic.Compatibility

Dim strBuff As New VB6.FixedLengthString(256)

但是它被标记为obsolete and specifically not supported for 64-bit processes,所以编写自己的复制功能,即截断设置长值并用空格填充右值。它还设置了一个未初始化的&#34;像上面一样,值为null。

来自LinqPad的示例代码(我不能允许Imports Microsoft.VisualBasic.Compatibility我认为因为它已被标记为过时,但我没有证明这一点:

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

有这个输出:

  

&#34; \ 0 \ 0 \ 0 \ 0 \ 0&#34;
  &#34;测试&#34;
  &#34; TESTI&#34;
  &#34;测试&#34;
  &#34; TESTI&#34;

答案 5 :(得分:1)

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

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

More

答案 6 :(得分:0)

试试这个:

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

创建一个填充“AAA ....”

的80个字符的字符串

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

    FileGet(1,strbuf)

将80个字符读入strbuf ...

答案 7 :(得分:-1)

你试过吗

Dim strBuff as String

另见Working with Strings in .NET using VB.NET

  

本教程将介绍如何操作   使用VB.NET表示.NET中的字符串   以及如何与他们合作   .NET类库类的帮助。

答案 8 :(得分:-1)

Dim a as string

a = ...

If a.length > theLength then

     a = Mid(a, 1, theLength)

End If

答案 9 :(得分:-1)

这尚未经过全面测试,但这里有一个解决这个问题的课程:

''' <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