Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I want to make sure I am only reading the numeric part of a response that is coming back as a string say "1.95e-3mA " to get this I been using substring to trim off the last part the darned board has just replied with "1.9e-3mA " meaning my careful
VB
result = dataBack.Substring(0,3) 

has broken and now I can see it all falling apart as the reply is now 1.9e (which breaks a calculation) rather than 1.95. Is there a simple way to do this or do I have to rethink the way I am doing this. Would Val() solve my issue?...
Glenn
Val() wont as I need to be a string I wonder Val it to get the number and the convert it back to a string???....
Posted
Updated 11-Apr-14 2:44am
v2
Comments
Thomas Daniels 11-Apr-14 8:47am    
Will the numeric part always end with e or not?
glennPattonWork3 11-Apr-14 8:49am    
Thought of that for this instrument yes, but for other instruments not always!

If the numeric part would always be followed by an e, then you could use String.Split[^] to split the string into an array and then take the first part of the array:
VB.NET
Dim input As String = "1.9e-3mA"
Dim result As String = input.Split("e"c)(0)

But you said in your comment that it won't always be an e, so you can use Regex.Split[^] to split by a regular expression:
VB.NET
Dim input As String = "1.9e-3mA"
Dim result As String = Regex.Split(input, "[^.\d-]")(0) ' first add Imports System.Text.RegularExpressions

This will split the input string at the chars that are not a digit or a dot, and then it takes the first part of the array to get the numeric part.
 
Share this answer
 
v4
Comments
glennPattonWork3 11-Apr-14 9:22am    
I am trying to use this code, but have fallen foul of Regex before (C#)! I have the Imports.System.Text.RegularExpressions, looking around MSDN is this supported in VB 2008?
It is complaining about RegularExpression not being a member of String...
Thomas Daniels 11-Apr-14 9:26am    
Yes, I think it is supported. By the way, you posted a reply to my comment (to your question) as a different comment, not as a reply. To reply to a comment, use the "Reply" button, not the "Have a Question or Comment?" button.
glennPattonWork3 11-Apr-14 9:29am    
Ooops! Sorry about that.
Thomas Daniels 11-Apr-14 9:32am    
No problem!
Thomas Daniels 11-Apr-14 9:29am    
If it is complaining about RegularExpression not being a member of String, then you probably have not used the static Split method of Regex. Can you show the code that you have now please?
Use a Regex! :laugh:
\d+(\.\d+)?([eE]-?\d+)?
Should do it - it matches the floating point number part only, and ignore anything else.

VB
'  Imports System.Text.RegularExpressions

'  Regular expression built for Visual Basic on: Fri, Apr 11, 2014, 01:54:44 PM
'  Using Expresso Version: 3.0.3634, http://www.ultrapico.com
'
'  A description of the regular expression:
'
'  Any digit, one or more repetitions
'  [1]: A numbered capture group. [\.\d+], zero or one repetitions
'      \.\d+
'          Literal .
'          Any digit, one or more repetitions
'  [2]: A numbered capture group. [[eE]-?\d+], zero or one repetitions
'      [eE]-?\d+
'          Any character in this class: [eE]
'          -, zero or one repetitions
'          Any digit, one or more repetitions
'
'

Public Dim regex As Regex = New Regex( _
      "\d+(\.\d+)?([eE]-?\d+)?", _
    RegexOptions.IgnoreCase _
    Or RegexOptions.CultureInvariant _
    Or RegexOptions.IgnorePatternWhitespace _
    Or RegexOptions.Compiled _
    )

' Capture the first Match, if any, in the InputText
Dim m As Match= regex.Match(InputText)
Dim result As string = m.Value
 
Share this answer
 
Comments
Thomas Daniels 11-Apr-14 9:06am    
This matches the whole floating-point part, 1.95e-3, but because of the SubString example of the OP, I think he only wants the 1.95 part.
IndexOf or LastIndexOf give the position of some otherstring inside the first string.
VB
position = databack.IndexOf("mA")

shows you where "mA" starts. If it is not present, IndexOf will return -1.
Assuming that it is always present, you could do
VB
result = databack.Substring(0,databack.IndexOf("mA"))
 
Share this answer
 
Comments
Thomas Daniels 11-Apr-14 9:08am    
This returns the whole floating-point part, 1.95e-3, but because of the SubString example of the OP, I think he only wants the 1.95 part.
glennPattonWork3 11-Apr-14 9:14am    
Yup! I most certainly do!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900