Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grab value from string in reverse order

Tags:

c#

.net

I have file with txt content inside. Content is generated dynamicly and I want to read in reverse order, from end of file to the first matched semicolon, for example:

sad12e1sadsadsadasdasd12e2q3312sdadasdasdasqe21231122123123asd1asda;123456

so I want to grab this 123456 integer, ofcourse this is generated content with random int length.

like image 672
panjo Avatar asked Mar 04 '26 02:03

panjo


2 Answers

If you have always the searched text at the end of the string and after a semicolon you could use

string.LastIndexOf(';');

for example

string test = "sad12e1sadsadsadasdasd12e2q3312sdadasdasdasqe21231122123123asd1asda;123456";
int pos = test.LastIndexOf(';');
if(pos >= 0)
   string myText = test.Substring(pos+1);
like image 90
Steve Avatar answered Mar 06 '26 16:03

Steve


What @Steve said, or just

string value = "sad12e1sadsadsadasdasd12e2q3312sdadasdasdasqe21231122123123asd1asda;123456";
string number = value.Split(';')[1];

though this doesn't handle the case where a semi-colon is missing.

like image 37
neontapir Avatar answered Mar 06 '26 14:03

neontapir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!