c# - 如何在 c# 中使用 VB6 InStr IntStRev 和 Mid
问题描述
我在一个项目中使用了 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
解决方案
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 )
从头开始”,没有必要,可以省略。注意 .NET(VB.NET 和 C#)中的字符串索引从 .NET 开始0
而不是1
.$
结尾Mid$
是 1980 年代早期 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 );
}
推荐阅读
- r - 如何在R中按条件分组?
- c# - 更改列表中的参考值
- angular - 在 Ng-Select 中更改颜色
- python - Django Signal.disconnect 引发“函数”对象没有属性“锁定”错误
- gnuradio - 连接到 git.gnuradio.org 时出现超时问题,如何解决?
- android - 使用 OpenCV4Android 覆盖图像
- c# - ServiceEventsClient 只接收最后一行事件数据
- pytorch - torch.Tensor.backward() 是如何工作的?
- python - numpy.zeros_like 中的订单类型
- python-3.x - 在计算单个数据帧后使用 Dask 返回多个数据帧