So I Have a List, but the list is in integer.
Like 1, 2, 3, 4, 5,...,10,...100,..
And I want to convert that integer to string with format 00000X
.
X is represent the integer. So the list will be 000001, 000002, 000010, 000100, etc
.
So I Have a List, but the list is in integer.
Like 1, 2, 3, 4, 5,...,10,...100,..
And I want to convert that integer to string with format 00000X
.
X is represent the integer. So the list will be 000001, 000002, 000010, 000100, etc
.
you can achive this by using LINQ. Please check below answer.
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 10, 100, 1000,10000,99999,100000 };
var mask = "00000";
List<string> stringNumbers = numbers.Select(x =>
{
if (mask.Length > x.ToString().Length)
{
return mask.Substring(0, mask.Length - x.ToString().Length) + x.ToString();
}
else return x.ToString();
}).ToList();
List<string> result = new List<string>();
foreach (int yourNumber in intList) {
result.Add("0000000".Substring(0, 7 - yourNumber.ToString().Length);
}
Sould work.
You also can do in on one line, intList.ForEach(...