Remove characters 0 to left of a string c#

Asked

Viewed 48 times

0

I came across a situation, I need to recover the user code that comes for example like this: '000063'. But my need was to take only the value of '63', use the . Trimstart would be a good option?

this.txtARQUIVO.Text = item.ALUNOID + "_" + item.CODESTAGIO.TrimStart('0') + ".jpg";
  • Yes, it’s the right option.

  • @Maniero would have a better way of asking the question to be in the scope of the site? is that not understand very well why is outside

  • I think not because the question can be answered with a yes or no. Unless a wrong answer has happened. It doesn’t help that the answer has a positive.

  • 1

    You can use Regex for this too, but with trimstart it’s easier... Regex RemoverZeros = new Regex(@"^0+(?=\d)"); Use: removeLeadingZeroesReg.Replace(item.CODESTAGIO, "")

1 answer

2


It can be Replace . Where you have 0000 replace with nothing

example:

item.CODESTAGIO.Replace("0000","")

this.txtARQUIVO.Text = item.ALUNOID + "_" + item.CODESTAGIO.Replace("0000","") + ".jpg";
  • 4

    This answer, besides being inefficient, does not solve cases where you have a different amount of zeros, for example 000163, so the answer is wrong.

  • 1

    is like a gambiarra, serves only for this case right! but really is a way out.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.