Click here to Skip to main content
15,358,946 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the following listed in a multiple line textbox in Visual Basic. If there are two leading zeros I need them removed but if only one zero exists, I need only one zero removed.

Start with this....

00012M12
00046W44
00280D18
00381F10
00509N01
00608T22
00879K17
01277P91

End with this....

012M12
046W44
280D18
381F10
509N01
608T22
879K17
1277P91


Thank you !!

What I have tried:

I can get the leading zeros removed but I'm stuck on the rest

VB
For i As Integer = 0 To TextBox1.Lines.Length - 1
            Dim index As Integer = TextBox1.GetFirstCharIndexFromLine(i)
            TextBox1.Select(index, Math.Min(1, TextBox1.Lines(i).Length))
            TextBox1.SelectedText = ""
       Next i
Posted
Updated 30-Mar-22 19:33pm

Simplest solution is check is the string starts with "00" and if so remove them. If not, check for "0" and remove that:
VB
Private Shared Function StripLeadingZeros(ByVal s As String) As String
    If s.StartsWith("00") Then
        s = s.Substring(2)
    ElseIf s.StartsWith("0") Then
        s = s.Substring(1)
    End If

    Return s
End Function
   
Try the following code:
VB
Dim strInput = "00012M12"
Dim Tcount = strInput.TakeWhile(Function(c) c = "0"c).Count()
strInput = strInput.TrimStart("0"c)
If Tcount Mod 2 > 0 Then strInput = "0" & strInput
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900