473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Fast way for extracting tokens from a string.


Hello,
I have a string formated in the following way:

s = 1;32;100;32;09;.........;09;76;

I need to extract the numbers separated by the seicolon.
The list can contain several thousands of items and the code is
time critical.

How can i best extract them in C#.

Many Thanks in advance

JB
Nov 17 '05 #1
5 4154
Jensen,

I would just use the Split method on the string itself, passing a
semicolon. It's probably going to give you the fastest performance.

However, I don't know that you should use a string at all. If the
string is rather large, you should be parsing it apart as you are retrieving
the information. For example, if the string was in a file, or being read
over a stream, I would parse it out as I read the characters from the
stream, not once the string was constructed.

If you got it from someplace else, like a database field (where it is in
string format already) and you can't do anything about it being in a string,
use the Split method.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jensen bredal" <je************@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

Hello,
I have a string formated in the following way:

s = 1;32;100;32;09;.........;09;76;

I need to extract the numbers separated by the seicolon.
The list can contain several thousands of items and the code is
time critical.

How can i best extract them in C#.

Many Thanks in advance

JB

Nov 17 '05 #2
Many thanks...
String comes from database.


Nov 17 '05 #3
"Nicholas Paldino [.NET/C# MVP]" wrote:
I would just use the Split method on the string itself, passing a
semicolon. It's probably going to give you the fastest performance.


That would not be my expectation. Split is going to create "several
thousands of" little strings, greatly increasing memory pressure. This
is a classic case where enumerating through Regex.Matches should be
faster than creating an array of strings. Plus a regex could ignore
white space, check the tokens are all digits, &c.

--

www.midnightbeach.com
Nov 17 '05 #4
could you provide some sample code?
Nov 17 '05 #5
Jensen bredal wrote:

could you provide some sample code?


<semicode> Your sample string

s = 1;32;100;32;09;.........;09;76;

contains a stream of digits followed by semicolons. The regex

(\d+);

will match a stream of digits followed by semicolons, capturing the
digits. This capture is only a matter of calculating string length and
start offset - no substring operations are done until you read a Value
property. So, do a foreach on Regex.Matches() of your data and the
@"(\d+);" regex pattern. Read the 2nd group in each match - that's got
the captured digits, without the semicolon.

</semicode>

--

www.midnightbeach.com
Nov 17 '05 #6

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

Similar topics

7
by: nephish | last post by:
Hey there, i have a text file with a bunch of values scattered throughout it. i am needing to pull out a value that is in parenthesis right after a certain word, like the first time the word...
4
by: Jay Nesbitt | last post by:
Is there a quick way to extract tokens from a string of text? I know Java provides a StringTokenizer class to do this. Is there something similar in the dot net framework? Ex. string s = "some...
4
by: Alexis Gallagher | last post by:
(I tried to post this yesterday but I think my ISP ate it. Apologies if this is a double-post.) Is it possible to do very fast string processing in python? My bioinformatics application needs to...
7
by: Tempo | last post by:
Hello. I am having a little trouble extracting text from a string. The string that I am dealing with is pasted below, and I want to extract the prices that are contained in the string below. Thanks...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.