Zero detector at variable start

Hi everyone

I have a problem, I need to know how I can detect if a value has a zero at the beginning

example:

variable = 1
and since it doesn’t have a zero at the beginning, I need

note = string variable

and I also want it to be vice versa for other cases

example:

variable = 01
turn it to 1

1 Like

@borismh

I guess what you are trying to say is that you want to eliminate the “0” at the beginning of the integer variable.

eg:-
input- 02
Output - 2

input - 03456
output - 3456

input- 00004
output - 4

Please confirm and let me know if this is what you want.

If above is the case, all you can do is:-

string input_variable = 093

int Result = Cint(input_variable)

If you try printing the value of the variable Result in the message box, you will get the value 93.

Thanks and Regards,
@hacky

2 Likes

Hi @borismh

You can read this article to address your issue.

cheers :smiley:

Happy learning :smiley:

6 Likes

@hacky

if I want to detect first if it has a zero at the beginning of the variable and from that point delete it or create a zero at the beginning

@borismh, If I understood correctly, you want to delete “0” from string and return the result. If so you can use replace method.

e.g. stringVariableName = stringVariableName.Replace(“0”, “”)

1 Like

@Bhavik_Solanki

ok, but to detect if it has a zero at the beginning, since this would be my conditioner

@borismh, if you are ok with first character you can use substring method.

E.g. stringVariableName.Substring(0,1) = “0”

1 Like

@Bhavik_Solanki

ready until that part is done, but now I just want to add 0 to variables that are only from 1 to 9, there is some way thank you very much

@borismh

Please try using variable.TrimStart("0"c)

This will trim any leading 0s from the value.

2 Likes

if you only need the number without “0” on the left you only have to make a convertion to double or int like this =
variable1= Cint(“01”)
now variable1 will be equal to 1 and you can convert it to string again
strVariable = variable1.toString()

Now you can continue doing whatever you want with it

1 Like

Here’s a solution for removing leading zeroes with regular expressions.

One zero
System.IO.RegularExpressions.Regex.Replace("^0", String.Empty)

All leading zeroes
System.IO.RegularExpressions.Regex.Replace("^0+", String.Empty)

3 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.