I have a simple string variable as follows and I want to get last line from this text.
string mytext ="line1\nline2\nline3";
Please do not offer to use
var lastLine = File.ReadLines("file.txt").Last();
Because the target string is not stored in the file.
Thanks for your help
Split by newline and then do last:
var lastLine = mytext.Split('\n').Last();
Try this:
int idx = mytext.LastIndexOf('\n');
if (idx != -1)
{
var rsult = mytext.Substring(idx + 1);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With