Solved

Parsing a string

Posted on 2006-07-12
11
194 Views
Last Modified: 2010-04-23
HELLO EXPERTS,

here is the string i am trying to parse, from a flat file,

str =  "115+0000000000000101EA117"
I need to get the 10 out of this but sometimes the string could be

"115+0000000000002401EA115" which I need 240.

How can i strip out all the zeros but know how many zeros. Thanks
0
Comment
Question by:lytung818
[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
  • 5
  • 3
  • 3
11 Comments
 
LVL 10

Expert Comment

by:vadim63
ID: 17095804
Dim NewStr as String = str.Substring(16, 3).TrimStart("0")
0
 

Author Comment

by:lytung818
ID: 17095835
why substring(16,3)? like i mentioned it could be 240 or 10..
0
 
LVL 8

Accepted Solution

by:
Shakti109 earned 250 total points
ID: 17095855

Use regular expressions as the following :

     Dim rs As New Regex("[+]{1}[0]*")
      Dim rc As Match
      Dim ZeroCount As Integer
      Dim tmpstr As String
      Dim tmpstr2 As String()

      For Each rc In rs.Matches(TextBox1.Text)
         ZeroCount = Len(Mid(rc.ToString, 1, Len(rc.ToString) - 1))
         tmpstr = Mid(TextBox1.Text, ZeroCount + 5, Len(rc.ToString))
      Next

       tmpstr2 = Split(tmpstr, "1EA")

      console.writeline ("Zeros : " & zerocount)
      console.writeline ("My Number : " & tmpstr2(0)

To test this :

Create a new form, put a textbox on the form and a button.
In the button.click event, put the above code.

Run the application and In the textbox, put in one of your strings :
I used 5 different ones :
115+0000000000000401EA118
115+0000000000002401EA118
115+0000000000000021EA118
115+0000000000999401EA118
115+0000000000010401EA118

You will get the right "pieces" each time.

0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 10

Expert Comment

by:vadim63
ID: 17095861
substring(16,3) returns "010" or "240" from your original str.
0
 
LVL 8

Expert Comment

by:Shakti109
ID: 17095866

My code above, assumes the following :

1) Your strings ALWAYS contain that "+" character as the delimiter as to where the zeros start.
2) Your strings ALWAYS contain "1EA" as the delimiter as to where the "significant number" STOPS.

Do you have other variants of these strings besides the above conditions or do they always look like that pattern?
0
 
LVL 8

Expert Comment

by:Shakti109
ID: 17095870

And whoops, I left out a right brace in the last line :

Replace  : console.writeline ("My Number : " & tmpstr2(0)

WITH : console.writeline ("My Number : " & tmpstr2(0))
0
 
LVL 10

Expert Comment

by:vadim63
ID: 17095875
Or even:
Dim NewStr As String = str.Substring(4, 15).TrimStart("0")
0
 
LVL 8

Expert Comment

by:Shakti109
ID: 17095885

As an aside, you will need to import the regex namespace to be able to use them.

You do this by using :

Imports system.text.regularexpressions

You can import this globally to your application, OR just in the form/module you are going use it.

0
 

Author Comment

by:lytung818
ID: 17096025
Shakti109, your function worked perfectly!! thank you!
0
 
LVL 8

Expert Comment

by:Shakti109
ID: 17096193

You are very welcome!

Research regular expressions a little, you will be very surprised what they can do.

You could actually use a single complex regular expression for this, OR, you could break the string into a couple pieces and use a regular expression on THAT.

I chose to craft this solution in this way so you could see the application of several pieces of string manipulation :

Regular expressions, "split" and "mid".

This should give you a few interesting things to explore with regard to string manipulation, enjoy!
0
 

Author Comment

by:lytung818
ID: 17110917
Shakti109
, regular expressions seem so complicated, how do i find the number 14 in this? there are two +

 115+0000000000000101EA117+00000000000000141UM1UP1084871870375

thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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