Question

I figure regex is overkill also it takes me some time to write some code (i guess i should learn now that i know some regex).

Whats the simplest way to separate the string in an alphanumeric string? It will always be LLLLDDDDD. I only want the letters(l's), typically its only 1 or 2 letters.

Was it helpful?

Solution

TrimEnd:

string result = input.TrimEnd(new char[]{'0','1','2','3','4','5','6','7','8','9'});
// I'm sure using LINQ and Range can simplify that.
// also note that a string like "abc123def456" would result in "abc123def"

But a RegEx is also simple:

string result = Regex.Match(input,@"^[^\d]+").Value;

OTHER TIPS

I prefer Michael Stum's regex answer, but here's a LINQ approach as well:

string input = "ABCD1234";
string result = new string(input.TakeWhile(c => Char.IsLetter(c)).ToArray());

You can use a regular expression that matches the digits to remove them:

input = Regex.Replace(input, "\d+$", String.Empty);

The old fashioned loop isn't bad either, it should actually be the fastest solution:

int len = input.Length;
while (input[len-1] >= '0' && input[len-1] <= '9') len--;
input = input.Substring(0, len);

They've got it - note the good solutions use the not operator to employ your problem description: "Not numbers" if you had the numbers at the front seems from my limited gains that you have to have what is called capturing groups to get past whatever it is on the front-end of the string. The design paradigm I use now is not delimiter character, followed by delimiter character, followed by an opening brace.

That results in needing a delimiter character that is not in the result set, which for one thing can be well established ascii values for data-delimitersl eg 0x0019 / 0x0018 and so on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top