如何在 C# 中使用 StrDup

Ley*_*Ley 2 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
Run Code Online (Sandbox Code Playgroud)

对于 StrDup 当我用 C# 编写代码时,与下面的代码相同:

字符串 newRN = 新字符串 (int.Parse(RN.Substring(2, 1)),'0');

它显示错误。

Dav*_*jas 5

使用带有字符和计数参数的字符串构造函数:

string newRN = new string('0', 4);
Run Code Online (Sandbox Code Playgroud)

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