Parsing a string in C # after reading the first and last alphabet

I want to cut a string in C # after reading the first and last alphabet.

string name = "20150910000549659ABCD000007348summary.pdf";

 string result = "ABCD000007348"; // Something like this string name = "1234 ABCD000007348 summary.pdf"; 

After reading 1234 "A" comes and finally "comes", so I want " ABCD000007348 "

+5
source share
2 answers

Just use Regex:

 string CutString(string input) { Match result = Regex.Match(input, @"[a-zA-Z]+[0-9]+"); return result.Value; } 
+3
source

Since you did not say that it was always a timestamp at the beginning, I decided instead to iterate over the string to find the first alphabetical character, not hardcoding s.Remove(0, n); , where n, however, many digits are in the timestamp.

 string s = "20150910000549659ABCD000007348summary.pdf"; s = s.Replace("summary.pdf", String.Empty); int firstLetter = 0; foreach (char c in s) { if (Char.IsLetter(c)) { firstLetter = s.IndexOf(c); break; } } s = s.Remove(0, firstLetter); 
0
source

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


All Articles