gpt4 book ai didi

c# - 格式化来自 Web API 的响应

转载 作者:行者123 更新时间:2023-11-30 20:32:32 25 4
gpt4 key购买 nike

我有一个 Web API 运行一个存储过程并返回表中的记录。该记录包括名为 CounterSeq 的 int 字段

[HttpGet]
public IHttpActionResult Get(string Account)
{
// other code for connecting to the SQL seerver and calling the stored procedure
reader = command.ExecuteReader();
List<QueryResult>qresults = new List<QueryResult>();
while (reader.Read())
{
QueryResult qr = new QueryResult();
qr.AccountID = reader["AccountID"].ToString();
qr.CounterSeq = reader["CounterSeq"].ToString();
qresults.Add(qr);
}
DbConnection.Close();
return Ok(qresults);

响应中 CounterSeq 的格式应该是 001。如果我在下面添加他这样的语句

  qr.CounterSeq ="00"+ reader["CounterSeq"].ToString();

但是即使对于两位数,它也显示为“0012”,但它必须是“012”我该如何处理

最佳答案

你可以使用 PadLeft

 qr.CounterSeq = reader["CounterSeq"].ToString().PadLeft(3, '0');

Returns a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode character.

关于c# - 格式化来自 Web API 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41288478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com