Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Parsing a string

Posted on 2006-07-12
11
Medium Priority
?
197 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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!

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 …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

636 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