Solved

Removing zeros in a string using VB.NET?

Posted on 2004-08-26
12
275 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

12 Experts available now in Live!

Get 1:1 Help Now