473,804 Members | 2,136 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
22 1515
I wrote:
Also, accessing array items inside a loop should be avoided whenever
possible.
And then Scott M. wrote:
That's ridiculous! Where did you get that from? One of the benefits of
arrays and collections is the ease of iterating through them via loops.
Your code is perfectly fine, but please don't make up best practices just to
bolster your code over others.
I'm not trying to bolster anything over anyone, just givin' away advise
that I gathered along the road. You're free to accept it or not.

Considering the tone of you message, and knowing that I can really
propose ridiculous things without noticing, I took the time to call
ILDasm on your code. This is what I got inside its main loop (I'm not
even talking about using Split here, that required the conversion of
the "-" char into an array of chars. The things you learn with
ILDasm...):
For i = 0 To y(0).Length - 1
If IsNumeric(y(0)( i)) Then z &= y(0)(i)
; Locates the element Y(0) and pushes it on the stack
IL_002d: ldloc.2
IL_002e: ldc.i4.0
IL_002f: ldelem.ref

; Gets the i-th char from the element
; on the stack (y(0)(i))
IL_0030: ldloc.0 ;
IL_0031: callvirt char String::get_Cha rs(int32)

; Boxes the char from the previous step and
; calls IsNumeric(Objec t) (oops!)
IL_0036: box System.Char ;
IL_003b: call bool IsNumeric(objec t)

;Skips the code if the result of the
;previous step was false
IL_0040: brfalse.s IL_0057

; Pushes z on the stack
IL_0042: ldloc.3

; locates (again!), the element y(0)(i):
; pushes y(0) onto the stack
IL_0043: ldloc.2
IL_0044: ldc.i4.0
IL_0045: ldelem.ref

;gets the i-th char
IL_0046: ldloc.0
IL_0047: callvirt char String::get_Cha rs(int32)

;Converts it to String (!!!)
IL_004c: call string ToString(char)

; concatenates z and the previous string (creating another string)
; and points z to it
IL_0051: call string String::Concat( string, string)
IL_0056: stloc.3
Next
As you can see, accessing y(0)(i) is somewhat inneficient, because the
code will have to locate the base element by index twice, and the char
by index twice, at every loop. This means locating this element ten
times, for the current example. I guess you'd agree that I can locate
the element only once *outside* the loop, and the given char only once
per cicle (inside the loop). That's exactly what happens when you use
an enumeration on a string, so I really preffer this approach than
accessing an indexed element inside a loop, anytime (unless, of course,
I need the index for something else).

As for the string concatenation, two new strings are created at each
loop cicle, one from the char and one as the result of the
concatenation. To produce your five-chars string you created at least
10 temporary strings. I don't know about you but I don't think this is
efficient at all... Now, I can only guess how the StringBuilder works,
but I'm positive it doesn't use temporary strings, but more likely an
efficient array of chars that will be resized fewer times than you
created strings (if resized at all).

Finally, knowing that IsNumeric() boxes its parameter will really make
me stay far away from it (unless, of course, I need it's VB6 semantics,
which is hardly the case, nowadays).

Hope this clarifies things a bit.

Of course, you can allways argue that the string you're manipulating is
so small that the number of resources and the time taken to proccess it
is negligible, and maybe I'll aggree with you on this. But, who knows,
the OP's example may or may not reflect his actual string. Besides,
there's no information on how many other strings he has to handle.

Best regards,

Branco.

Aug 19 '06 #21
I never claimed that my code was the most efficient way of doing this. What
I did say was that your statement of avoiding arrays inside loops whenever
possible is a ridiculous statement to broadly make. Perhaps it is not the
most efficient way of handling this particular scenario, but, in general,
arrays and collections are prime candidates for iteration via loops. What
makes this particular example less efficient is that there are 2 indexes to
deal with, rather than just the one. In cases where there is just one index
to deal with, or in situations where there are just too many values to write
individual lines of code for, loops are most certainly the way to go with
arrays and collections.

As for the use of StringBuilders, if you read my last response, you must
take into account the base size of a StringBuilder vs. the memory used by
creating new String objects. Again, this particular example may be best
handled with SB's, and I never said SB's were NOT the way to go. What I
said was that because of the intern pool, you can certainly wind up with
situations where using a SB would create more overhead than not using one.

I do agree with your philosophy on advice though, so I hope you don't mind
if I use it:

Just givin' away advice that I gathered along the road. You're free to
accept it or not.

:)
"Branco Medeiros" <br************ *@gmail.comwrot e in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
>I wrote:
>Also, accessing array items inside a loop should be avoided whenever
possible.

And then Scott M. wrote:
>That's ridiculous! Where did you get that from? One of the benefits of
arrays and collections is the ease of iterating through them via loops.
Your code is perfectly fine, but please don't make up best practices just
to
bolster your code over others.

I'm not trying to bolster anything over anyone, just givin' away advise
that I gathered along the road. You're free to accept it or not.

Considering the tone of you message, and knowing that I can really
propose ridiculous things without noticing, I took the time to call
ILDasm on your code. This is what I got inside its main loop (I'm not
even talking about using Split here, that required the conversion of
the "-" char into an array of chars. The things you learn with
ILDasm...):
>For i = 0 To y(0).Length - 1
> If IsNumeric(y(0)( i)) Then z &= y(0)(i)
; Locates the element Y(0) and pushes it on the stack
IL_002d: ldloc.2
IL_002e: ldc.i4.0
IL_002f: ldelem.ref

; Gets the i-th char from the element
; on the stack (y(0)(i))
IL_0030: ldloc.0 ;
IL_0031: callvirt char String::get_Cha rs(int32)

; Boxes the char from the previous step and
; calls IsNumeric(Objec t) (oops!)
IL_0036: box System.Char ;
IL_003b: call bool IsNumeric(objec t)

;Skips the code if the result of the
;previous step was false
IL_0040: brfalse.s IL_0057

; Pushes z on the stack
IL_0042: ldloc.3

; locates (again!), the element y(0)(i):
; pushes y(0) onto the stack
IL_0043: ldloc.2
IL_0044: ldc.i4.0
IL_0045: ldelem.ref

;gets the i-th char
IL_0046: ldloc.0
IL_0047: callvirt char String::get_Cha rs(int32)

;Converts it to String (!!!)
IL_004c: call string ToString(char)

; concatenates z and the previous string (creating another string)
; and points z to it
IL_0051: call string String::Concat( string, string)
IL_0056: stloc.3
>Next

As you can see, accessing y(0)(i) is somewhat inneficient, because the
code will have to locate the base element by index twice, and the char
by index twice, at every loop. This means locating this element ten
times, for the current example. I guess you'd agree that I can locate
the element only once *outside* the loop, and the given char only once
per cicle (inside the loop). That's exactly what happens when you use
an enumeration on a string, so I really preffer this approach than
accessing an indexed element inside a loop, anytime (unless, of course,
I need the index for something else).

As for the string concatenation, two new strings are created at each
loop cicle, one from the char and one as the result of the
concatenation. To produce your five-chars string you created at least
10 temporary strings. I don't know about you but I don't think this is
efficient at all... Now, I can only guess how the StringBuilder works,
but I'm positive it doesn't use temporary strings, but more likely an
efficient array of chars that will be resized fewer times than you
created strings (if resized at all).

Finally, knowing that IsNumeric() boxes its parameter will really make
me stay far away from it (unless, of course, I need it's VB6 semantics,
which is hardly the case, nowadays).

Hope this clarifies things a bit.

Of course, you can allways argue that the string you're manipulating is
so small that the number of resources and the time taken to proccess it
is negligible, and maybe I'll aggree with you on this. But, who knows,
the OP's example may or may not reflect his actual string. Besides,
there's no information on how many other strings he has to handle.

Best regards,

Branco.

Aug 20 '06 #22
Scott M. wrote:
I never claimed that my code was the most efficient way of doing this. What
I did say was that your statement of avoiding arrays inside loops whenever
possible is a ridiculous statement to broadly make.
I suppose I owe you an apology. I guessed it was clear that I was
refering to your perdulary use of y(0)(i) inside the loop, not the
general issue of "arrays inside loops". This point seemed so basic to
me that needed not an ellaboration and thus the "avoid using array
inside loops" advice, which is clearly misphrased -- and I apologize
for the misunderstandin g. My actual point was, if you must access an
array index inside a loop (and, of course, arrays were born to live
inside loops), try at least to minimize the access to the same
elements. Using y(0)(i) twice in the same expression just for the sake
of saving a line of code seems amatteurish to me (in the bad sense),
and, really, not wise. Also, it spells to novices a certain sloppy
style of coding that smells like leaving option strict off, using
Object instead of strong typing, etc.

;-)

Best regards,

Branco.

Aug 20 '06 #23

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
2254
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
4013
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
5272
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
2694
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
4889
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
4529
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
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10603
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
10353
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...
0
10099
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
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
6869
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
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
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

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.