Solved

Removing zeros in a string using VB.NET?

Posted on 2004-08-26
12
295 Views
Last Modified: 2010-04-23
Hi,

I have a string as

dim myNumber1 as string = "0024"
dim myNumber2 as string = "02400"

i am trying to remove the starting zeros in a string ..

the above should return as
myNumber1   = 24
myNumber2  = 2400

thanks
0
Comment
Question by:praveen79
12 Comments
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 49 total points
ID: 11901495
Somethig like this:

        Dim s As String = "002400"
        Dim i As Integer = CInt(s)

the var 'i' will contain the value 2400.
as you may keep the values in the string use:

        Dim s As String = CInt("002400").ToString


Best regards,
Alex
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 11901633
Check TrimStart function
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 11901757
Yeah... that also works but...

I just don't like that aproach that much because if the string is entered in a textbox the can exist som 'spaces" like:

dim myNumber1 as string = "  0024"
dim myNumber2 as string = " 02400 "

In my aproach these 'errors' are ignored, in iboutchkine's the string will remain exactly the same, even creating the char() on the trimstart with more than one item it's a bit tricky...

Resume:
If there's no chance of apearing spaces on the expression use the TrimStart... or else use my aproach...

Best regards,
Alex
0
 
LVL 2

Expert Comment

by:Mehrdad_Y
ID: 11901966
Hi, the easiest solution is:

dim myNumber1 as string = "0024"
myNumber1=myNumber1.Replace("0","")
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 11902223
No buddy... wrong aswer..

What abou if the string number is "002400"?
It'll return "24" not "2400" as expected!

Alex
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:Mehrdad_Y
ID: 11902779
You're right, sorry, Let's try this one:

Public Function RemovePreZeros(S As String) As String
     Dim ans As String
     Dim i As Integer
     For i=0 To S.Length -1
          If S.SubString(i,1)<>"0" Then
               ans=S.Subtring(i)
               Exit For
          End If
     Next i
     Return ans
End Function
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 11903090
hoooooooooo...

One of the good things in VB is that we can do complicated stuff with few lines of code...
I belienve that not even C need so many lines to do that! :p

Alex :p
0
 
LVL 41

Expert Comment

by:graye
ID: 11914370
iboutchkine has the right answer...

Use a combination of Trim() to get rid of any leading/trailing whitespace, then use TrimStart() to get rid of the leading zeros.

     Dim s as string = "  00123  "

     s = s.trim.trimstart("0"c)

1 line of code!
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 11914645
Nice...

But I think I like Mehrdad_Y's answer best! :D:D:D
0
 
LVL 2

Expert Comment

by:Mehrdad_Y
ID: 11916162
Thanks Alex
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now