Skip to main content
added 313 characters in body
Source Link
King King
  • 63.5k
  • 16
  • 118
  • 140

You can use RegEx for this:

var parts = Regex.Split(textBox1.Text, "(\\d+)"); textBox1.Text = parts.Length<2 ? parts[0] : parts[0] + parts[1].PadLeft(5, '0'); 

Update: if you don't want to use Regex, you can try the following code:

int i = 0; for(;i < textBox1.TextLength; i++){ if(char.IsDigit(textBox1.Text[i])) break; } textBox1.Text = textBox1.Text.Substring(0,i) + textBox1.Text.Substring(i).PadLeft(5,'0'); 

You can use RegEx for this:

var parts = Regex.Split(textBox1.Text, "(\\d+)"); textBox1.Text = parts.Length<2 ? parts[0] : parts[0] + parts[1].PadLeft(5, '0'); 

You can use RegEx for this:

var parts = Regex.Split(textBox1.Text, "(\\d+)"); textBox1.Text = parts.Length<2 ? parts[0] : parts[0] + parts[1].PadLeft(5, '0'); 

Update: if you don't want to use Regex, you can try the following code:

int i = 0; for(;i < textBox1.TextLength; i++){ if(char.IsDigit(textBox1.Text[i])) break; } textBox1.Text = textBox1.Text.Substring(0,i) + textBox1.Text.Substring(i).PadLeft(5,'0'); 
deleted 310 characters in body
Source Link
King King
  • 63.5k
  • 16
  • 118
  • 140

You can use RegEx for this:

var parts = Regex.Split(textBox1.Text, "(\\d+)"); textBox1.Text = parts[0] + parts[1].PadLeft(5 - parts[0].Length, '0'); 

The code above works only if the text part has length of less than 5. You should check the Text like this:

textBox1.Text = textBox1.TextLength >= 5 ? textBox1.Text.Substring(0,5) : parts.Length < 2Length<2 ? parts[0] :  parts[0] + parts[1].PadLeft(5 - parts[0].Length, '0'); 

You can use RegEx for this:

var parts = Regex.Split(textBox1.Text, "(\\d+)"); textBox1.Text = parts[0] + parts[1].PadLeft(5 - parts[0].Length, '0'); 

The code above works only if the text part has length of less than 5. You should check the Text like this:

textBox1.Text = textBox1.TextLength >= 5 ? textBox1.Text.Substring(0,5) : parts.Length < 2 ? parts[0] :  parts[0] + parts[1].PadLeft(5 - parts[0].Length, '0'); 

You can use RegEx for this:

var parts = Regex.Split(textBox1.Text, "(\\d+)"); textBox1.Text = parts.Length<2 ? parts[0] : parts[0] + parts[1].PadLeft(5, '0'); 
added 225 characters in body
Source Link
King King
  • 63.5k
  • 16
  • 118
  • 140

You can use RegEx for this:

var parts = Regex.Split(textBox1.Text, "(\\d+)"); textBox1.Text = parts[0] + parts[1].PadLeft(5 - parts[0].Length, '0'); 

The code above works only if the text part has length of less than 5. You should check the Text like this:

textBox1.Text = textBox1.TextLength >= 5 ? textBox1.Text.Substring(0,5) : parts[1]parts.PadLeft(5Length -< 2 ? parts[0].Length, '0');:   parts[0] + parts[1].PadLeft(5 - parts[0].Length, '0'); 

You can use RegEx for this:

var parts = Regex.Split(textBox1.Text, "(\\d+)"); textBox1.Text = parts[0] + parts[1].PadLeft(5 - parts[0].Length, '0'); 

You can use RegEx for this:

var parts = Regex.Split(textBox1.Text, "(\\d+)"); textBox1.Text = parts[0] + parts[1].PadLeft(5 - parts[0].Length, '0'); 

The code above works only if the text part has length of less than 5. You should check the Text like this:

textBox1.Text = textBox1.TextLength >= 5 ? textBox1.Text.Substring(0,5) : parts.Length < 2 ? parts[0] :   parts[0] + parts[1].PadLeft(5 - parts[0].Length, '0'); 
Source Link
King King
  • 63.5k
  • 16
  • 118
  • 140
Loading