C# snippet needed to replicate VBA Like operator

后端 未结 3 1513
一向
一向 2021-01-12 14:46

I am converting VBA code that contains the LIKE operator, as in

    dim sToken as String
    if sToken Like \"(*,*)\" then ...

In all case

3条回答
  •  余生分开走
    2021-01-12 15:46

    Several people have suggested regular expressions which should work well for this scenario. Another option is to use the VB Like operator directly from C# code. This can be done by invoking the Compiler helper LikeOperator.LikeString. This function is located in the VB runtime assembly, Microsoft.VisualBasic.dll, and is usable from C#.

    using Microsoft.VisualBasic.CompilerServices;
    
    ...
    
    if (LikeOperator.LikeString(sToken, "(*,*)")) { 
      ...
    }
    

    I don't believe this version has 100% parity with the VBA version of Like but it will be extremely close and will match for the common scenarios.

提交回复
热议问题