3
How to check and fill in a string if its size is less than one condition? For example, I always need to have a string in the size of 8 or 9. If the string is longer I give a substring taking only the first 9 characters. If she’s a minor, I need to fill in "0" zeros on the left. Example : '988554' should be '000988554'. Important to keep in string format and not convert.
int TamanhoDaString= Regex.Replace(minhaString, "[^0-9]", "").Length; //ex: tamanho 5
int QuantDeZero = 9 - TamanhoDaString; // resultado = 4
int i;
string zeros = "0";
for (i = 1; i < QuantDeZero; i++)
{
// aqui engatei, pois como vou concatenar uma string com inteiro?
//resultado teria que ser zeros = "0000"
}
Have you tried
numero.ToString().PadLeft(9, '0')
?– Valdeir Psr