代码之家  ›  专栏  ›  技术社区  ›  DaveWilliamson

Visual Studio-如何在vb中查找字符串等于求值。网络代码

  •  2
  • DaveWilliamson  · 技术社区  · 8 年前

    我需要转换一堆VB。NET代码转换为C#。Instant C#是一个很好的帮手,但有一个方面。。。字符串不区分大小写。NET代码。

    例如,将测试例程更改为Test2例程的代表,然后转换为C#。

    Option Compare Text
    .
    .
    .
          Private Sub Test()
            Dim x as string = "x"
            Dim y as string = "y"
    
            If x = y then msgbox("They match")
    
          End Sub
    
          Private Sub Test2()
            Dim x as string = "x"
            Dim y as string = "y"
    
            If String.Compare(x, y, StringComparison.CurrentCultureIgnoreCase) = 0 then msgbox("They match")
    
          End Sub
    

    C#转换后的输出如下

      private static void Test()
      {
        string x = "x";
        string y = "y";
    
        if ((x ?? string.Empty) == (y ?? string.Empty))
        {
          MessageBox.Show("They match");
        }
    
      }
    
      private static void Test2()
      {
        string x = "x";
        string y = "y";
    
        if (string.Compare(x, y, StringComparison.CurrentCultureIgnoreCase) == 0)
        {
          MessageBox.Show("They match");
        }
    
      }
    

      private static void Test()
      {
        string x = "x";
        string y = "y";
    
        if (string.Equals(x ?? string.Empty, y ?? string.Empty, StringComparison.OrdinalIgnoreCase))
        {
          MessageBox.Show("They match");
        }
    
      }
    
    0 回复  |  直到 8 年前