split string

P

Peter K

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
 
J

Jon Skeet [C# MVP]

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))
{
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
 
D

DeveloperX

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top