我想在 c# WPF 中創建返回 10 個數字值的函數。它包含如下一位數(靜態)-> 'G'兩位數(動態)-> 當前年份的“19”兩位數(動態)-> 當前月份的“04”五位數字(動態)-> '00284' <- 從 sql 表返回。這個的長度必須是固定的。上面代碼的返回值是'G190400284'(我想要這個值作為返回值)如果我的五位數(4.)值應該是1然后它返回'G1904000001'
1 回答

嚕嚕噠
TA貢獻1784條經驗 獲得超7個贊
像那樣的東西?
using System;
public class Program
{
public static void Main()
{
Console.WriteLine(CreateString(1));
Console.WriteLine(CreateString(284));
}
public static string CreateString(int id)
{
var n = DateTime.Now;
return "G" + n.Year.ToString().Substring(2,2) + n.Month.ToString().PadLeft(2,'0') + id.ToString().PadLeft(5,'0');
}
}
// This returns
// G190400001
// G190400284
https://dotnetfiddle.net/oteEpe
- 1 回答
- 0 關注
- 83 瀏覽
添加回答
舉報
0/150
提交