String manipulation help

Hi all

I need help ignoring zeros at the beginning of a line

Example:
000000051385138
000017961796
The number of zeros is always different

And if it started with example: D00001526 to leave also

@Aibek_Abubakirov Is your Number to be extracted always 4 Digits ?

Hi @Aibek_Abubakirov,

If the number —> 00000005138 is your string then simply convert it into Int32 by using CInt(“00000005138”) it’ll automatically eliminate those zeros.

Use this in Try catch… so if string value is “D00001526” then it’ll throw error and you should do nothing in catch, to leave that particular value.

no always different

Hi @Aibek_Abubakirov,

Converting to Int32 will Remove the Preeding Zeroes . But since you have a Scenarios where the string can start from alphabets - there Convert to string will Fail. Hence we have Put the Conversion in trycatch block - This will Handle the exception without chnaging the source Data.


Mukesh

I can’t, because sometimes there is a string
Example D00001526,F000018545

@Aibek_Abubakirov

ok so, SO you can use try catch there.

Hi.
Did you try using replace of regex?
ex : text.replace(/0/gi,“”)

no, in this case it will not change all zeros?

@Aibek_Abubakirov Check if this Expression works :
System.Text.RegularExpressions.Regex.Match(“D00001526”,“[1-9][0-9]+”).ToString

You can test the expression for all kinds of Data that you have using below link

Oh, I didn’t think about it.

But
If the string contains letters, then it should not do anything with it.

@Aibek_Abubakirov,

Yes… so use CInt(StringVar) in Try catch… so if string value is “D00001526” then it’ll throw error and you should do nothing in catch, to leave that particular value as it is.

@Aibek_Abubakirov If you don’t want to Consider the numbers with values having Letters at the beginning, then you can use Cint(yourString) as suggested by @mukeshkala and @samir . That Should work

There is a variable that passes document number,
If it contains zeros at the beginning, then it should ignore them.

And if there is a letter in it, then I must skip

Hi @Aibek_Abubakirov,

You can Put the Conversion in Try catch Block as I suggested above ,Please share issues in that Approach.

Hi,

Checking if the string is numeric

You can use Information.IsNumeric(yourVariable) to check the string.

Your result will be:

result = IIf(Information.IsNumeric(yourVariable), yourVariable, yourVariable.TrimStart("0"C)

Solution to your question

Simply use TrimStart

yourVariable.TrimStart("0"C)


image

Thank you all very much