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