猿问

在 C# 中创建返回值的函数

我想在 c# WPF 中创建返回 10 个数字值的函数。它包含如下

  1. 一位数(静态)-> 'G'

  2. 两位数(动态)-> 当前年份的“19”

  3. 两位数(动态)-> 当前月份的“04”

  4. 五位数字(动态)-> '00284' <- 从 sql 表返回。这个的长度必须是固定的。

上面代码的返回值是'G190400284'(我想要这个值作为返回值)如果我的五位数(4.)值应该是1然后它返回'G1904000001'


慕后森
浏览 67回答 1
1回答

噜噜哒

像那样的东西?using System;public class Program{&nbsp; &nbsp; public static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(CreateString(1));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(CreateString(284));&nbsp; &nbsp; }&nbsp; &nbsp; public static string CreateString(int id)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var n = DateTime.Now;&nbsp; &nbsp; &nbsp; &nbsp; return "G" + n.Year.ToString().Substring(2,2) + n.Month.ToString().PadLeft(2,'0') + id.ToString().PadLeft(5,'0');&nbsp; &nbsp; }}// This returns// G190400001// G190400284https://dotnetfiddle.net/oteEpe
随时随地看视频慕课网APP
我要回答