Simple get string (ignore numbers at the end) in C #

I believe the regex is too large, and it takes me a while to write the code (I think I should now know that I know some regex).

What is the easiest way to separate a string in an alphanumeric string? It will always be LLLLDDDDD. I need only letters (l), usually it is only 1 or 2 letters.

+4
source share
4 answers

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 RegEx is also simple:

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

I prefer Michael Stam's regular expression answer, but here's the LINQ approach too:

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

You can use a regular expression that matches the numbers to delete them:

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

The old-fashioned cycle is also good, it should be the fastest solution:

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

They have it - note that good solutions are used by the operator not to use your description of the problem: โ€œNot numbersโ€, if you had numbers on the front, it seems, due to my limited advantages, that you should have the so-called capture groups to get past what is at the front end of the line. The design paradigm I'm using right now is not a separator character, followed by a separator character, followed by an opening bracket.

This makes it necessary to use a delimiter character that is not in the result set, which, on the one hand, can be well-established ascii values โ€‹โ€‹for data delimiters, for example 0x0019 / 0x0018, etc.

0
source

Source: https://habr.com/ru/post/1208330/


All Articles