在C#中转换VbScript函数(Right,Len,IsNumeric,CInt)

时间:2011-10-13 10:34:40

标签: c# vbscript

同样,我在VbScript中获得了以下代码,请您在C#中建议相同的代码。

Function GetNavID(Title)
    getNavID=UCase(Left(Title, InStr(Title, ". ") -1))
End Function

我已经从上一个问题改变了上面的代码,即

public static string GetNavID(string Title)
{
    int index = Title.IndexOf(". ");
    return Title.Substring(0, index - 1).ToUpper();
} 

现在我想在c#中转换下面的代码,因为有很多VBScript函数,所以感到困惑。

Dim NavigationId 'As String

NavigationId = GetNavID(oPage.Title)

' Is it a subnavigation member page ?
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  

请建议!!

4 个答案:

答案 0 :(得分:1)

尝试:

if (NavigationId.StartsWith("S"))
{
    NavigationId = NavigationId.Substring(1);
    int id;
    if (int.TryParse(NavigationId,out id))
    {
        if (id > 0)
        {
        }
    }
}

答案 1 :(得分:0)

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

转换为:

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

但是,您应该查看两种语言的字符串操作工作方式。

答案 2 :(得分:0)

if (NavigationId.StartsWith("S"))
{
    NavigationId = NavigationId.TrimStart("S");
    int temp = 0;
    if (int.TryParse(NavigationId, out temp))
    {
        if (temp > 0)
        {
            //Do something
        }     
    } 
}

答案 3 :(得分:0)

您应该在elseif部分throw上添加exception

string navigationIdString = GetNavID(oPage.Title)

if (navigationIdString.StartWith("S"))
{
    var navigationID = 0;
    if (int.TryParse(navigationIdString.SubString(1), navigationID)
    {
         if(navigationID > 0)
         {
             ...
         } 
    }
}