String manipulation help

Hi all

I need help ignoring zeros at the beginning of a line

Example:
00000005138 --> 5138
00001796 -> 1796
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 ?

1 Like

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.

1 Like

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

2 Likes

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

@Aibek_Abubakirov

ok so, SO you can use try catch there.

1 Like

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

1 Like

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

1 Like

Oh, I didn’t think about it.

1 Like

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.

2 Likes

@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

1 Like

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.

1 Like

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
image

3 Likes

Thank you all very much

1 Like

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