Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

in my opinion it is a common scenario

the string is something like :

"this is my story, if it's interesting [email protected] so thanks for your time"

i need to make it somthing like

"this is my story, if it's interesting  so thanks for your time"
"[email protected]"

my code for now is trying to count Down from the index of the "@" so it is less times to check within the iteration of the for loop

        public string formatResultContentAndEmail(string source)
        {
            char[] Str2CahrArr = source.ToCharArray();
            var trgt = source.IndexOf('@');
            var stepsBack=0;
            for (int i = trgt; i >0; i--)
            {
                var test = Str2CahrArr[i];
                if (Str2CahrArr[i].Equals(" "))
                {
                    stepsBack = i; break;
                }
            }

            return "";//<======change this when done tests
        }

my first problem in that try was i couldn't find when it hits a space .

but even when i'll solve that problem, is that approach is the right one ?

what is the simplest way to extract the mail substring of that complete paragraph ?

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

Perhaps there is a better regex approach which searches real emails, this is readable and efficient:

string text = "this is my story, if it's interesting [email protected] so thanks for your time";
if(text.Contains('@'))
{
    string[] words = text.Split();
    string[] emails = words.Where(word => word.Contains('@')).ToArray();
    text = string.Join(" ", words.Where(word => !word.Contains('@')));
}

Demo

this is my story, if it's interesting so thanks for your time
[email protected]
share|improve this answer
    
yep that sure looks more human, than others regex, and i guess in my case is the best to use . thanks for thinking simple.. if it is (: –  LoneXcoder May 31 '13 at 23:31
    
and if i could I'd give you another +1 for that new(at least for me) online ide benchmarker / test environment –  LoneXcoder May 31 '13 at 23:38
add comment
public string[] ExtractEmails(string str)
{
    string RegexPattern = @"\b[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}\b";

    // Find matches
    System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(str, RegexPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);

    string[] MatchList = new string[matches.Count];

    // add each match
    foreach (System.Text.RegularExpressions.Match match in matches)
        MatchList[c] = match.ToString();

    return MatchList;
}

Source: http://coderbuddy.wordpress.com/2009/10/31/coder-buddyc-code-to-extract-email/

If you need a better regular expression pattern, you can probably find one at http://www.regular-expressions.info/

share|improve this answer
    
Beat me to it. Regex is the clear best approach to this. –  J... May 31 '13 at 23:25
    
i guess i'll get more comfertable with Regex in the futere, but for now i will firstly test @Tim approach . Thanks, i sure will look in to that later on though! –  LoneXcoder May 31 '13 at 23:35
add comment

My suggestion would be as follows, using String.split()

public String getMail(String inp) {
  String[] prts = inp.split(" ");
  for(String tmp : prts) {
    if(tmp.contains("@")) {
      return tmp;
    }
  }
}

This will break on strings with more than one email, but the fix for that should be trivial.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.