如何在C#中使用StrDup

时间:2016-02-01 03:47:49

标签: c# vb.net

您好我有一个与VB.net一起使用的代码:

Public Shared Function GetNextID(ByVal Prefix As String) As String
    Dim dt As DataTable = Database.GetDataTable("select * from _NumberGeneration Where Entity = '" & Prefix & "'")
    If dt Is Nothing OrElse dt.Rows.Count = 0 Then Return ""
    Dim format As String = dt.Rows(0)("Format") & ""
    Dim sqlCnt As String = dt.Rows(0)("SQL") & ""
    Dim cnt As Int32 = Val(Database.ExecuteScalar(sqlCnt)) + 1  ' 11
    Dim RN As String = format.Substring(format.IndexOf("["c), 4) ' [R5]
    Dim newRN As String = StrDup(CInt(RN.Substring(2,1), "0"c) ' 00000
    newRN = cnt.ToString(newRN) ' String.Format(newRN, cnt) ' 00011
    Dim newformat As String = format.Replace(RN, newRN)  '"PR"yyMM00011
    Return Today.ToString(newformat) ' String.Format(newformat, cnt)
End Function

对于StrDup当我用C#编写代码时,代码如下:

string newRN = new string(int.Parse(RN.Substring(2,1)),'0');

显示错误。

1 个答案:

答案 0 :(得分:1)

将字符串构造函数与字符和计数参数一起使用:

string newRN = new string('0', 4);

您还可以引用Microsoft.VisualBasic程序集并通过Strings模块调用该方法,但上面的替代方法非常简单。