?
Solved

Removing zeros in a string using VB.NET?

Posted on 2004-08-26
12
Medium Priority
?
326 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 196 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
Industry Leaders: 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!

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

764 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