I am trying to extract something from an email. The general format of the email will always be:
blablablablabllabla hello my friend. [what I want] Goodbye my friend blablablabla
Now I did:
string.LastIndexOf("hello my friend"); string.IndexOf("Goodbye my friend");
This will give me a point before it starts, and a point after it starts. What method can I use for this? I found:
String.Substring(Int32, Int32)
But this only takes the start position.
What can I use?
Substring takes the start index (zero-based) and the number of characters you want to copy.
You'll need to do some math, like this:
string email = "Bla bla hello my friend THIS IS THE STUFF I WANTGoodbye my friend"; int startPos = email.LastIndexOf("hello my friend") + "hello my friend".Length + 1; int length = email.IndexOf("Goodbye my friend") - startPos; string sub = email.Substring(startPos, length);
You probably want to put the string constants in a const string
.
you can also use Regex
string s = Regex.Match(yourinput, @"hello my friend(.+)Goodbye my friend", RegexOptions.Singleline) .Groups[1].Value;
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