Converting VbScript functions (Right, Len, IsNumeric, CInt) in C#

后端 未结 4 716
死守一世寂寞
死守一世寂寞 2021-01-29 08:19

Again, I have got below code in VbScript, can you please suggest what will be equivalent code in C#.

Function GetNavID(Tit         


        
相关标签:
4条回答
  • 2021-01-29 08:24
    if (NavigationId.StartsWith("S"))
    {
        NavigationId = NavigationId.TrimStart("S");
        int temp = 0;
        if (int.TryParse(NavigationId, out temp))
        {
            if (temp > 0)
            {
                //Do something
            }     
        } 
    }
    
    0 讨论(0)
  • 2021-01-29 08:40

    You should probably put some elses on the ifs and throw some exceptions

    string navigationIdString = GetNavID(oPage.Title)
    
    if (navigationIdString.StartWith("S"))
    {
        var navigationID = 0;
        if (int.TryParse(navigationIdString.SubString(1), navigationID)
        {
             if(navigationID > 0)
             {
                 ...
             } 
        }
    }
    
    0 讨论(0)
  • 2021-01-29 08:43

    Try:

    if (NavigationId.StartsWith("S"))
    {
        NavigationId = NavigationId.Substring(1);
        int id;
        if (int.TryParse(NavigationId,out id))
        {
            if (id > 0)
            {
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-29 08:49
    If Left(NavigationId, 1) = "S" Then
        NavigationId = Right(NavigationId, Len(NavigationId) - 1)           
        If IsNumeric(NavigationId) Then
           ' Its a subnavigation non-index page "Sxxx"
           If CInt(NavigationId) > 0 Then
    
           End If
        End If
    End If
    

    Translates into:

    if(NavigationId.StartsWith("S")) {
        NavigationId = NavigationId.Substring(1); 
        int navId;
        if(Int32.TryParse(NavigationId, out navId) && navId > 0) {
           // Do what you need to do.
        }
    }
    

    However, you should look into the string manipulation workings of both languages.

    0 讨论(0)
提交回复
热议问题