Hi All
If anyone has the time, I think feedback on the following may be of interest
to all:
I have to build a lot of bank files which are generally of the fixed length
field type eg aba format. The beauty of the old VB6 Mid statement (which is
available in VB.Net) is that it allows me to replace n chars at a specified
position in a string eg
'(air code)
Private Enum MyFields
mfRecordType = 1
mfTransactionType = 2
'etc
End Enum
Dim buffer as New String(120, " "c)
Dim pointer as Integer = 1
Mid(buffer, pointer, MyFields.mfRecordType) = "A"
pointer += MyFields.mfRecordType
Mid(buffer, pointer, MyFields.mfTransactionType ) = "AB"
pointer += mfTransactionType 'etc etc
There appears no native VB.Net equivalent to this method. The StringBuilder
Class does not have this method either. It should be noted that the Replace
method for either String or StringBuilder would not be appropriate for the
above.
The above algorithm that I use will actually write several thousands records
to file in very fast time (around or under 1 second)
As I have stated, this whole question is of an academic nature. Having said
that, is there any equivalent method (as used above) for the Mid Statement
in VS (any language)?
Thanks for your time.
To summerize, I put together a function myself and then included Armin's and
the VB.NET mid statement method:
Here is the output (release mode build, no debugger attached) - total time for
30000000 iterations each:
Tom's StringBuilder AppendField extension: 00:00:03.8856089
Armin's StringBuilder AppendWithPadding extension: 00:00:05.6895440
VB.NET mid statement: 00:00:14.5048549
Press any key to continue . . .
And here is the code:
Option Strict On
Option Explicit On
Imports System.Text
Imports System.Runtime.CompilerServices
Module Module1
Enum Fields
First = 1
Second = 3
Third = 1
End Enum
Const RecordLength As Integer = 5
Sub Main()
Dim sw As New Stopwatch()
Dim buffer As New StringBuilder(RecordLength, RecordLength)
sw.Start()
For i As Integer = 1 To 30000000
buffer.Length = 0
buffer.AppendField(Fields.First, "A")
buffer.AppendField(Fields.Second, "CD")
buffer.AppendField(Fields.Third, "EF")
Next
sw.Stop()
Console.WriteLine("Tom's StringBuilder AppendField extension: {0}", sw.Elapsed)
sw.Reset()
Dim sb As New System.Text.StringBuilder(RecordLength, RecordLength)
sw.Start()
For i As Integer = 1 To 30000000
sb.Length = 0
sb.AddWithPadding("A", Fields.First)
sb.AddWithPadding("CD", Fields.Second)
sb.AddWithPadding("E", Fields.Third) ' armins causes an error if this is longer then the max lenght
Next
sw.Stop()
Console.WriteLine("Armin's StringBuilder AppendWithPadding extension: {0}", sw.Elapsed)
sw.Reset()
sw.Start()
For i As Integer = 1 To 30000000
Dim ptr As Integer = 1
Dim sbuffer As New String(" "c, RecordLength)
Mid(sbuffer, ptr, Fields.First) = "A"
ptr += Fields.First
Mid(sbuffer, ptr, Fields.Second) = "CD"
ptr += Fields.Second
Mid(sbuffer, ptr, Fields.Third) = "EF"
Next
sw.Stop()
Console.WriteLine("VB.NET mid statement: {0}", sw.Elapsed)
End Sub
<Extension()> _
Sub AppendField(ByVal buffer As StringBuilder, ByVal maxLength As Integer, ByVal value As String)
For i As Integer = 0 To maxLength - 1
If i < value.Length Then
buffer.Append(value(i))
Else
buffer.Append(" "c)
End If
Next
End Sub
<Runtime.CompilerServices.Extension()> _
Sub AddWithPadding(ByVal sb As System.Text.StringBuilder, ByVal Text As String, ByVal TotalLength As Integer)
sb.Append(Text)
sb.Append(New String(" "c, TotalLength - Text.Length))
End Sub
End Module