Solved

Removing zeros in a string using VB.NET?

Posted on 2004-08-26
12
324 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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