首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在c#中使用VB6 InStr IntStRev和Mid

如何在c#中使用VB6 InStr IntStRev和Mid
EN

Stack Overflow用户
提问于 2018-06-02 06:11:31
回答 1查看 879关注 0票数 1

我在一个项目中使用VB6,后来我切换到c#,我的VB6代码中有一个函数,我想在我的C#项目中使用。

也许String.Substring可以做到这一点。

这是我的VB6代码。

代码语言:javascript
复制
position = InStr(1, strout, "4000072")

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

回答 1

Stack Overflow用户

发布于 2018-06-02 06:19:30

代码语言:javascript
复制
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 );

在您的案例中:

代码语言:javascript
复制
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.
  • The ( VB.NET和C#)中,字符串索引从0开始,而不是从0开始。$ in end of Mid$是20世纪80年代VB早期遗留下来的陈旧东西,即使在VB6中也没有必要。另外,您的If缺少其End If.

所以:

代码语言:javascript
复制
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 );
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50651546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档