我希望在for循环中保留变量的三位数。 E.g。
for (int i = 002; i <= 033; i++)
{
string localFilename = @"\\psf\Home\Pictures\Maulavi\" + i + ".jpg";
using (WebClient client = new WebClient())
{
MessageBox.Show(i.ToString());
client.DownloadFile("http://eap.bl.uk/EAPDigitalItems/EAP566/EAP566_1_1_19_2-EAP566_Maulvi_January_1946_v43_no2_" + i + "_L.jpg", localFilename);
}
}
但我改为仅2
和3
等。我希望它保持为002
和003
等。我该怎么做?
答案 0 :(得分:2)
请尝试DROP PROCEDURE IF EXISTS sp_GetMonthVals;
DELIMITER //
CREATE PROCEDURE sp_GetMonthVals(IN StartDate DATETIME, IN EndDate DATETIME)
BEGIN
DECLARE CurrentDate DATETIME;
SET @CurrentDate = StartDate;
DROP TABLE IF EXISTS _tmpMonthVals;
CREATE TABLE _tmpMonthVals (`MDate` DATETIME, `DVal` int);
label1: WHILE @CurrentDate < EndDate DO
INSERT INTO _tmpMonthVals SELECT @CurrentDate, DVal FROM Table1 WHERE MDate <= @CurrentDate ORDER BY MDate DESC LIMIT 1;
SET @CurrentDate = DATE_ADD(@CurrentDate, INTERVAL 1 DAY);
END WHILE label1;
SELECT * FROM _tmpMonthVals;
END//
DELIMITER ;
CREATE TABLE IF NOT EXISTS Table1
(`id` int, `MDate` DATETIME, `DVal` int)
;
INSERT INTO Table1
(`id`, `MDate`, `DVal`)
VALUES
(1, '2015-10-17', 25),
(2, '2015-10-12', 20),
(3, '2015-10-01', 5);
CALL sp_GetMonthVals('2015-10-01', '2015-10-31');
,如下所示:
ToString("000")
答案 1 :(得分:1)
您可以i.ToString("D3")
或i.ToString("000")
。
有关数字到字符串转换的更多信息,请查看Standard Numeric Format Strings。
答案 2 :(得分:0)
使用函数ToString()
的重载格式化字符串:
for(int i = 2; i <= 33; i++)
{
string localFilename = @"\\psf\Home\Pictures\Maulavi\" + i.ToString("000") + ".jpg";
}
或者您可以使用Format
功能:
String.Format("{0:000}", i);
答案 3 :(得分:0)
使用string.Format(@“\ psf \ Home \ Pictures \ Maulavi {0:000} .jpg”,i)
格式也将比字符串连接具有更好的性能。
答案 4 :(得分:0)
请按照以下方式使用:
char padChar = '0';
for (int i = 002; i <= 033; i++)
{
string localFilename = @"\\psf\Home\Pictures\Maulavi\" + i + ".jpg";
string padStr = i.ToString().PadLeft(3,padChar);
using (WebClient client = new WebClient())
{
MessageBox.Show(i.ToString());
client.DownloadFile("http://eap.bl.uk/EAPDigitalItems/EAP566/EAP566_1_1_19_2-EAP566_Maulvi_January_1946_v43_no2_" + padStr + "_L.jpg", localFilename);
}
}
有关此次访问的详情,请访问:http://mahedee.net/padding-character-with-string/