Click here to Skip to main content
15,436,928 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have an Input string

"44/1,Cell: +91- 112222112 Mail:abcde@gmail.com"

my out put string needs to be "abcde@gmail.com"
How i can achieve this in C#
I need to remove all letters up to Mail:

What I have tried:

i tried Indexof()
For Indexof i need to replace "Mail:" with a character then i can get "abcde@gmail.com" using Sub string function
Posted
Updated 10-Oct-18 5:58am
Comments
Sergey Alexandrovich Kryukov 12-May-16 3:00am     CRLF
Regex! But, honestly, you did not formulate the problem at all. An example is not a formulation. —SA

try this, provided your input will be in this manner ( email at the last)

C#
string input = "44/1,Cell: +91- 112222112 Mail:abcde@gmail.com";
            string email = input.Substring(input.IndexOf("Mail:")).Replace("Mail:", "");
 
Share this answer
 
v2
Try:
C#
string testString = "44/1,Cell: +91- 112222112 Mail:abcde@gmail.com";
string sub = testString.Substring(testString.IndexOf("Mail:") + 5);
Console.WriteLine(sub);


Prior to that check this link[^] and this link[^] to understand how two very common String function works.

Hope it helps.
 
Share this answer
 
C#
string[] strAry = "44/1,Cell: +91- 112222112 Mail:abcde@gmail.com".Split(':');

            foreach (var item in strAry)
            {
                bool isEmail = Regex.IsMatch(item, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase);
                if (isEmail)
                {
                    MessageBox.Show(item);
                }
            }
 
Share this answer
 
Comments
Richard Deeming 11-Oct-18 10:04am    
Read the question again. The OP was not asking about validating an email address!
use method index of for string..
find first occurance of Mail: you will get the position of M.
like:


string s1="44/1,Cell: +91- 112222112 Mail:abcde@gmail.com";
int k=s1.IndexOf("Mail:");


then u can use substring method from k+1 index
 
Share this answer
 
Comments
Richard Deeming 11-Oct-18 10:03am     CRLF
Already covered in solution 2. And your code won't remove the "Mail:" prefix, which the OP didn't want in the output string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900