Solved

Removing zeros in a string using VB.NET?

Posted on 2004-08-26
12
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

697 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