Solved

Parsing a string

Posted on 2006-07-12
11
190 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

15 Experts available now in Live!

Get 1:1 Help Now