Aman Ali
Aman Ali

Reputation: 61

How do I use VB6 InStr IntStRev And Mid in c#

I was using VB6 for a project later I switched to c# I have a function in my VB6 code I wanna use in my C# Project.

String.Substring maybe could do this.

Here is my VB6 Code.

position = InStr(1, strout, "4000072")

If position > 0 Then
    position2 = InStrRev(strout, "0000", position)
    strout = Mid$(strout, position2 - 512, 512)
End If

Upvotes: 2

Views: 1886

Answers (1)

Dai
Dai

Reputation: 155250

VB6: index = InStr( haystack, needle )
C# : index = haystack.IndexOf( needle );

VB6: index = InStrRev( haystack, needle )
C# : index = haystack.LastIndexOf( needle );

VB6: substring = Mid( largerString, start, length )
C# : substring = largerString.Substring( start, length );

In your case:

position = InStr(1, strout, "4000072")
If position > 0 Then
    position2 = InStrRev(strout, "0000", position)
    strout = Mid$(strout, position2 - 512, 512)
  • The 1 in InStr( 1, haystack, needle ) means "Start at the beginning", it's unnecessary so it can be ommitted. Note in .NET (both VB.NET and C#) string indexes start at 0 instead of 1.
  • The $ at the end of Mid$ is an archaic holdover from VB's very early days in the 1980s that isn't necessary even in VB6. Also your If is missing its End If.

So:

Int32 index400 = strout.IndexOf( "4000072" );
if( index400 > -1 ) {
    index000 = strout.LastIndexOf( "00000", index400 );

    Int32 start = Math.Max( index0000 - 512, 0 );
    Int32 length = Math.Min( 512, index0000.Length - start );  
    strout = strout.Substring( start, length );
}

Upvotes: 1

Related Questions