Solved

Parsing a string

Posted on 2006-07-12
11
193 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
  • 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

820 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