Extract string between two characters using SubString and RegularExpression in C#

Last Reply 2 months ago By kalpesh

Posted 2 months ago

My String is

for example

"xyz abc" <xyzabc2571997@gmail.com>

i want xyzabc2571997@gmail.com



"riya vyas" <riyavyas1997@gmail.com>

i want riyavyas1997@gmail.com

in c# website

You are viewing reply posted by: kalpesh 2 months ago.
Posted 2 months ago Modified on 2 months ago

Try by below code if you string contain only one email details.

string testText = @"""riya vyas"" <riyavyas1997@gmail.com>";
int startindex = testText.IndexOf('<');
int Endindex = testText.IndexOf('>');
string outputstring = testText.Substring(startindex + 1, Endindex - startindex - 1);

If its multiple then you can store it in string array variable like below.

string input = @"""xyz abc"" <xyzabc2571997@gmail.com>""riya vyas"" <riyavyas1997@gmail.com>";
string[] testing = Regex.Matches(input, @"\<(.+?)\>")
                            .Select(s => s.Groups[1].Value).ToArray();


I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html