473,320 Members | 2,012 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.

split string

Hi

I am looking for an elegant method for splitting strings with the following
format into its separate parts:

<id><language><version>

id is always 32 characters long;
language is a representation of language/locale (eg. "en" or "da-DK");
version is always a number (eg. "3" or "17" or "665");

for example:
a29b4372003c2345719de23a63725102da-DK86
a29b4372003c2345719de23a63725102en4

Obviously obtaining the first 32 characters is easy. But I need a nice way
of getting the language and version substrings. At the moment I start at
the end and work backwards until I find the first non-digit character (then
I know I have the version substring). But is there a better way?
Thanks,
Peter
Apr 3 '07 #1
3 1587
On Apr 3, 8:16 am, Peter K <xdz...@hotmail.comwrote:
I am looking for an elegant method for splitting strings with the following
format into its separate parts:

<id><language><version>

id is always 32 characters long;
language is a representation of language/locale (eg. "en" or "da-DK");
version is always a number (eg. "3" or "17" or "665");

for example:
a29b4372003c2345719de23a63725102da-DK86
a29b4372003c2345719de23a63725102en4

Obviously obtaining the first 32 characters is easy. But I need a nice way
of getting the language and version substrings. At the moment I start at
the end and work backwards until I find the first non-digit character (then
I know I have the version substring). But is there a better way?
Well, I'd stick to using Substring for the first 32 characters, but
you *could* use a regular expression for splitting the language and
version. Alternatively, you could use String.IndexOfAny and Substring
again. I suspect that would be clearer, but may not perform as well.
The version which would probably perform best would be more
longwinded:

string id = data.Substring(0, 32);
for (i=32; i < data.Length; i++)
{
if (char.IsDigit(data[i]))
{
string language = data.Substring(32, 32-i);
string version = data.Substring(i);
// Do whatever and return
}
}
// If you get to here, you haven't found anything - error?

I'd go with whatever you find clearest unless performance is really
important, in which case try everything and measure!

Jon

Apr 3 '07 #2
On 3 Apr, 08:30, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On Apr 3, 8:16 am, Peter K <xdz...@hotmail.comwrote:

I'd go with whatever you find clearest unless performance is really
important, in which case try everything and measure!

Jon- Hide quoted text -

- Show quoted text -
Can I say measure twice and split once ;) Sorry couldn't resist,
carpentry humour meets c# at last :)

There's a typo in the above: Try

string language = data.Substring(32 , i-32);

Cheers

Apr 3 '07 #3
DeveloperX <nn*****@operamail.comwrote:

<snip>
There's a typo in the above: Try

string language = data.Substring(32 , i-32);
Whoops - yes, thanks very much :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 3 '07 #4

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

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
6
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
4
by: Itzik | last post by:
can i split this string string str = "aa a - bb-b - ccc" with this delimiter string del = " - " i want recieve 3 items : "aa a" , "bb-b" , "ccc"
4
by: Crirus | last post by:
There is a function somewhere to split a string with multiple tokens at a time? Say I have this: aaaa#bbbbb*ccccc$dddd I whould like to split it so the result whould be aaaa bbb
14
by: Ron | last post by:
Hello, I am trying to parse a string on the newline char. I guess vbCrLf is a string constant. How can I parse my string - data - on the newline char? .... data += ASCII.GetString(buffer, 0,...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
2
by: Digital Fart | last post by:
following code would split a string "a != b" into 2 strings "a" and "b". but is there a way to know what seperator was used? string charSeparators = { "=", ">=", "<=" , "!=" }; string s1 =...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.