473,796 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parsing between a character and sysmbol

I have a string like the following:

10AF101-25

I would like to extract any numerical number that precedes the "-" and
stops when it encounters any string character like AF

So my result should be 101.
Any help is appreciated it.

Aug 17 '06 #1
22 1512
Dim x As String = "10AF101-25"
Dim y As String() = x.split("-")
Dim z As String

dim i As Integer
For i = 0 To y(0).length
If IsNumeric(y(i)) Then z = y &= y(i)
Next

<ge*********@gm ail.comwrote in message
news:11******** **************@ 75g2000cwc.goog legroups.com...
>I have a string like the following:

10AF101-25

I would like to extract any numerical number that precedes the "-" and
stops when it encounters any string character like AF

So my result should be 101.
Any help is appreciated it.

Aug 17 '06 #2

ge*********@gma il.com wrote:
I have a string like the following:

10AF101-25

I would like to extract any numerical number that precedes the "-" and
stops when it encounters any string character like AF

So my result should be 101.
<snip>

The function bellow may help:

Function ScanInt(ByVal T As String) As String
'Finds the position of the "-",
'counting from the last char
Dim P As Integer = T.LastIndexOf("-")

'Scans the text backward until
'a non noumeric item is found
Dim I As Integer
For I = P - 1 To 0 Step -1
If Not Char.IsDigit(T( I)) Then
Exit For
End If
Next

'I is referencing the first non-digit char
I += 1
If I < P Then
Return T.Substring(I, P - I)
Else
Return String.Empty
End If
End Function

Regards,

B.

Aug 17 '06 #3
This seems much more complicated than it need be. Why search for the "-"
when you can immediately split the string at its location and throw away the
part you don't need?

"Branco Medeiros" <br************ *@gmail.comwrot e in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
>
ge*********@gma il.com wrote:
>I have a string like the following:

10AF101-25

I would like to extract any numerical number that precedes the "-" and
stops when it encounters any string character like AF

So my result should be 101.
<snip>

The function bellow may help:

Function ScanInt(ByVal T As String) As String
'Finds the position of the "-",
'counting from the last char
Dim P As Integer = T.LastIndexOf("-")

'Scans the text backward until
'a non noumeric item is found
Dim I As Integer
For I = P - 1 To 0 Step -1
If Not Char.IsDigit(T( I)) Then
Exit For
End If
Next

'I is referencing the first non-digit char
I += 1
If I < P Then
Return T.Substring(I, P - I)
Else
Return String.Empty
End If
End Function

Regards,

B.

Aug 17 '06 #4
adm

Scott M. wrote:
This seems much more complicated than it need be.
I think regular expressions might be the way to go here. This kind of
pattern matching is what they are designed for.

To get started, see:
http://www.regular-expressions.info/dotnet.html
http://weblogs.asp.net/rosherove/story/6863.aspx
>
"Branco Medeiros" <br************ *@gmail.comwrot e in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .

ge*********@gma il.com wrote:
I have a string like the following:

10AF101-25

I would like to extract any numerical number that precedes the "-" and
stops when it encounters any string character like AF

So my result should be 101.
<snip>

The function bellow may help:

Function ScanInt(ByVal T As String) As String
'Finds the position of the "-",
'counting from the last char
Dim P As Integer = T.LastIndexOf("-")

'Scans the text backward until
'a non noumeric item is found
Dim I As Integer
For I = P - 1 To 0 Step -1
If Not Char.IsDigit(T( I)) Then
Exit For
End If
Next

'I is referencing the first non-digit char
I += 1
If I < P Then
Return T.Substring(I, P - I)
Else
Return String.Empty
End If
End Function

Regards,

B.
Aug 17 '06 #5
Hello Scott M.,

There are several things wrong with your code.
First, Using IsNumeric can lead to anomolous results.
Second, your loop bounds are not correct and will cause an exception.
Third, Your loop scans forward, so the result from the example string will
be "10", not the desired "101".

I would have to agree with adm, RegEx is designed specifically for this very
thing. It excels at it.
Grab a copy of Expresso.

-Boo
Dim x As String = "10AF101-25"
Dim y As String() = x.split("-")
Dim z As String
dim i As Integer
For i = 0 To y(0).length
If IsNumeric(y(i)) Then z = y &= y(i)
Next
<ge*********@gm ail.comwrote in message
news:11******** **************@ 75g2000cwc.goog legroups.com...
>I have a string like the following:

10AF101-25

I would like to extract any numerical number that precedes the "-"
and stops when it encounters any string character like AF

So my result should be 101.

Any help is appreciated it.

Aug 18 '06 #6

ge*********@gma il.com wrote:
I have a string like the following:

10AF101-25

I would like to extract any numerical number that precedes the "-" and
stops when it encounters any string character like AF

So my result should be 101.
Any help is appreciated it.\
Another possible solution :)

Option Strict On
Option Explicit On

Imports System
Imports System.Text.Reg ularExpressions

Module RegExDemoModule
Private Const DEFAULT_STRING As String = "10AF101-25"
Private Const REGULAR_EXPRESS ION As String = "[a-zA-Z]+(\d+)-"

Sub Main()
Dim expression As New Regex(REGULAR_E XPRESSION)
Dim theMatch As Match = expression.Matc h(DEFAULT_STRIN G)

Console.WriteLi ne(theMatch.Gro ups(1).ToString ())
End Sub

End Module

--
Tom Shelton [MVP]

Aug 18 '06 #7

Scott M. wrote:
This seems much more complicated than it need be. Why search for the "-"
when you can immediately split the string at its location and throw away the
part you don't need?
Because split does just that: searches for a pattern. And then allocate
an array. And then populate each member of the array with a copy of the
slice between the separator.

It just bothers me to use Split to do that. Just as IsNumeric: it works
by converting the string to Double, what seems a terrible waste of
processing, to me.

The code seems too much effort but it also seems efficient, to me.
YMMV.

Regards,

Branco.

Aug 18 '06 #8
I would have to agree with adm, RegEx is designed specifically for this
very thing. It excels at it.
Grab a copy of Expresso.
But if you are not used to RegEx can a combination from the code from Scott
and Branco help you.

\\\
Dim x As String = "10AF101-25"
Dim y As String() = x.Split("-"c)
Dim i As Integer
For i = y(0).Length - 1 To 0 Step -1
If Not Char.IsDigit((y (0)(i))) Then
Exit For
End If
Next
MessageBox.Show (y(0).Substring (y(0).Length - 3))
///
Tested of course in this case,

And assuming that there is forever at leaset one hyphen in the value.

:-)

Cor
Aug 18 '06 #9
See my reply too Boo (GhostinAK)
"adm" <ad*****@yahoo. comschreef in bericht
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
>
Scott M. wrote:
>This seems much more complicated than it need be.

I think regular expressions might be the way to go here. This kind of
pattern matching is what they are designed for.

To get started, see:
http://www.regular-expressions.info/dotnet.html
http://weblogs.asp.net/rosherove/story/6863.aspx
>>
"Branco Medeiros" <br************ *@gmail.comwrot e in message
news:11******* *************** @i42g2000cwa.go oglegroups.com. ..
>
ge*********@gma il.com wrote:
I have a string like the following:

10AF101-25

I would like to extract any numerical number that precedes the "-" and
stops when it encounters any string character like AF

So my result should be 101.
<snip>

The function bellow may help:

Function ScanInt(ByVal T As String) As String
'Finds the position of the "-",
'counting from the last char
Dim P As Integer = T.LastIndexOf("-")

'Scans the text backward until
'a non noumeric item is found
Dim I As Integer
For I = P - 1 To 0 Step -1
If Not Char.IsDigit(T( I)) Then
Exit For
End If
Next

'I is referencing the first non-digit char
I += 1
If I < P Then
Return T.Substring(I, P - I)
Else
Return String.Empty
End If
End Function

Regards,

B.

Aug 18 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3002
by: Kylotan | last post by:
I have a text file where the fields are delimited in various different ways. For example, strings are terminated with a tilde, numbers are terminated with whitespace, and some identifiers are terminated with a newline. This means I can't effectively use split() except on a small scale. For most of the file I can just call one of several functions I wrote that read in just as much data as is required from the input string, and return the...
3
2253
by: Fuzzyman | last post by:
I want to parse some text and generate an output that is similar but not identical to the input. The string I produce will be of similar length to the input string - but a bit longer. I'm parsing character by character and adding the characters of the input string to the output until I come to ones I want to modify. This means creating a new string for every character (since strings are immutable) which seems very inneficient -...
19
4012
by: ARK | last post by:
I am writing a search program in ASP(VBScript). The user can enter keywords and press submit. The user can separate the keywords by spaces and/or commas and key words may contain plain words, single quoted strings (phrases), double quoted strings (phrases). For example: Keywords: Jack, Jill, Jim, "Timothy Brown", Mary OR
4
5268
by: Earl | last post by:
I'm curious if there are others who have a better method of accepting/parsing phone numbers. I've used a couple of different techniques that are functional but I can't really say that I'm totally happy with either. 1. My first technique was to restrict the users to entries that could only be 3 character, 3 characters, 4 character (area code, prefix, suffix, respectively). I would null out any inputs that were non-numeric (except the...
1
2693
by: David | last post by:
I have rows of 8 numerical values in a text file that I have to parse. Each value must occupy 10 spaces and can be either a decimal or an integer. // these are valid - each fit in a 10 character block 123.8 123.8 1234.567 12345 12345 1234.567
6
2078
by: Tuomas Rannikko | last post by:
Hello, I'm currently writing a XML processor for the fun of it. There is something I don't understand in the spec though. I'm obviously missing something important. The spec states that both Internal General and Character references are included when referenced in content. And "included" means: <quote>
2
4887
by: RG | last post by:
I am having trouble parsing the data I need from a Serial Port Buffer. I am sending info to a microcontroller that is being echoed back that I need to remove before I start the actual important data reading. For instance this is my buffer string: 012301234FFFFFFxFFFFFFxFFFFFFx Where the FFFFFF is my Hex data I need to read. I am using the "x" as a separater as I was having problems using the VbCrLf. But I think
13
4516
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple command set consisting of several single letter commands which take no arguments. A few additional single letter commands take arguments:
1
3504
by: Sidhartha | last post by:
Hi, I am facing a problem while parsing local language characters using sax parser. We use DOM to parse and SAX to read the source. But when our application parses strings with local language especially czech,polish,turkish in place of local language character some other word is comming. Eg: Input string :ahoj, jak se máš Output string :ahoj, jak se m&aacute;š
0
9683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10457
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10231
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.