Solved

Parsing a string

Posted on 2006-07-12
11
189 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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 this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now