Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove zeros from start of string 2

Status
Not open for further replies.

Syerston

Programmer
Jun 2, 2001
142
GB
What is the best way to remove the zeros from the start of a string. Unfortunately the string can contain characters as well as numbers.

Thanks in advance John
 

Just use a loop until the next character is not a zero:

Dim i As Integer
For i=1 to Len(myString)
If ASC(myString) = 48 Then myString = Right$(myString,Len(myString)-1)
Next i
 
You should also be able to use the Val() function. Try

MyString = "00023"
Text1 = Val(MyString)

'Val(MyString)' returns 23. Thanks and Good Luck!

zemp
 
Problem with that approach zemp is that you will lose the characters embedded within the string. Unfortunately we need to be sensitive to this complication given the conditions of the original question.

I think CCLINT's approach is in the right direction, the only thing I would do differently, given the expense of the Right function, is to loop thru and find the location of the first non-zero characters, then call the Right function one time. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
If I understand you corretly, you are saying that if the first non-zero character is an alpha character ("000C23")then the val() will not work correctly. I didn't consider that and you are correct.

Thanks for catching that and I stand corrected. Thanks and Good Luck!

zemp
 
Code:
Public Function basNoLeadZero(strIn As String) As String

    Dim Idx As Integer

    If (Len(strIn) = 0) Then
        basNoLeadZero = strIn
        Exit Function
    End If

    Idx = 1
    Do While Idx <= Len(strIn)
        If (Mid(strIn, Idx, 1) <> &quot;0&quot;) Then
            Exit Do
        End If
        Idx = Idx + 1
    Loop

    basNoLeadZero = Right(strIn, Len(strIn) - Idx + 1)

End Function
MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
After running some performance tests, I stand corrected.

Using the Mid function to find the starting point repeatedly is essential the same as repeatedly using the Right function in terms of performance.

The fastest code is an adapdation of CCLINT's code and is as follows:
Code:
TheLen = Len(TheString)
Do While (TheLen > 0)
   If (Asc(TheString) = 48) Then
      TheLen = TheLen - 1
      TheString = Right(TheString, TheLen)
   Else
      TheLen = 0
   End If
Loop
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Syerston: You need to give CajunCenturion a &quot;Thankyou&quot; and a star as I forgot to add an exit to the loop if the next character is not a zero, and CajunCenturion had caught this (I actually thought on it also, but at the end forgot to add the Exit):

Dim i As Integer
For i=1 to Len(myString)
If ASC(myString) = 48 Then
myString = Right$(myString,Len(myString)-1)
Else
Exit For

End If
Next i


While the advice offered by zemp is good, if the string contains only numbers, one needs to take care of international settings if the string contains fractions. This is where the Val() function fails. In this case, the Str$() function would work as a substitute though.
 
Cajun Centurion

A big thank you to you as well John
 
You're quite welcome, and thank you CCLINT. In addition to the loop exit, I also removed the calls to the Len function. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Will people please stop being nice to each other in this forum? It may set a precedent...
 
strongm:
[taking a deep breath][shocked]We just didn't realize...

Oh, sorry[sadeyes], Thank you[wavey3] so much for being so thoughtful][thumbsup2] and kind][roll2] and considerate[colorface] for pointing this out!
Your help and advice is appreciated[bigsmile] so much[medal] and we all hope you will accept out apologies[peace] and we give you a thousand hugs[spin] and kisses [love] for reminding us of that.[thumbsup]

XXXXXXXXXXXXX[lickface] OOOOOOOOOOO
[wavey2][wink]
Best[wavey] regards

[flowerface]



[rollling on the floor][laughtears]
 
I think I'm going to cry...

yep, I am going to cry [fish] No Dolphins were harmed in the posting of this message... Dolphin Friendly Tuna!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top