このような文字列を宣言するにはどうすればよいですか?
Dim strBuff As String * 256
VB.NETで?
VBFixedString 属性を使用します。ここでMSDN情報を参照してください
<VBFixedString(256)>Dim strBuff As String
この VB 6 コードを記述するには:
Dim strBuff As String * 256
VB.Net では、次のようなものを使用できます。
Dim strBuff(256) As Char
文字列ビルダーを使用する
'Declaration
Dim S As New System.Text.StringBuilder(256, 256)
'Adding text
S.append("abc")
'Reading text
S.tostring
これを試して:
Dim strbuf As New String("A", 80)
「AAA....」で満たされた 80 文字の文字列を作成します。
ここでは、バイナリ ファイルから 80 文字の文字列を読み取ります。
FileGet(1,strbuf)
80文字をstrbufに読み込みます...
このオブジェクトは、1 つのコンストラクターと 2 つのプロパティを持つ構造体として定義できます。
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/
使用できますMicrosoft.VisualBasic.Compatibility
:
Imports Microsoft.VisualBasic.Compatibility
Dim strBuff As New VB6.FixedLengthString(256)
しかし、それは古いものとしてマークされており、特に 64 ビット プロセスではサポートされていません。そのため、長い値を設定する際に切り捨て、短い値の場合はスペースで右にパディングするという機能を複製する独自のコードを作成してください。また、上記のように「初期化されていない」値を 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()
この出力があります:
"\0\0\0\0\0"
"テスト" "テスト"
"
テスト" "テスト
"
Dim a as string
a = ...
If a.length > theLength then
a = Mid(a, 1, theLength)
End If
やってみました
Dim strBuff as String
VB.NETを使用した.NETでの文字列の操作も参照してください。
このチュートリアルでは、VB.NETを使用して.NETで文字列を表現する方法と、.NETクラスライブラリクラスを使用して文字列を操作する方法について説明します。