我在一个项目中使用VB6,后来我切换到c#,我的VB6代码中有一个函数,我想在我的C#项目中使用。
也许String.Substring可以做到这一点。
这是我的VB6代码。
position = InStr(1, strout, "4000072")
If position > 0 Then
position2 = InStrRev(strout, "0000", position)
strout = Mid$(strout, position2 - 512, 512)
End If
发布于 2018-06-02 06:19:30
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 );
在您的案例中:
position = InStr(1, strout, "4000072")
If position > 0 Then
position2 = InStrRev(strout, "0000", position)
strout = Mid$(strout, position2 - 512, 512)
1
in InStr( 1, haystack, needle )
的意思是“从头开始”,这是不必要的,可以省略。注在1
.0
开始,而不是从0
开始。$
in end of Mid$
是20世纪80年代VB早期遗留下来的陈旧东西,即使在VB6中也没有必要。另外,您的If
缺少其End If
.所以:
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 );
}
https://stackoverflow.com/questions/50651546
复制相似问题