Split phone number that comes from an html file and split it in two

Asked

Viewed 113 times

2

I receive a numeric field in a html that comes like this "413232123441999993254" which in this case would be two phone numbers with DDD 41, both enter the Phone: field, I would like to divide these numbers in two to be more organized. I’m using C# for the development of this application, I already have the Phone method that captures this html the phone number I need right now split it in two but I know how to do it. Follows below phone method as it is today:

   public string Telefone(string pContent)
    {

        var content = BuscaSimples(pContent, 
            "Telefone: </span><span style=\"font-size: 18px; color:black;\">",
            "</span>");

        if (content.Empty())
            content = BuscaSimples(pContent, "<a href=\"tel:\" style=\"color:#0082c8\">", "</a>");

        if (content.Empty())
            content = BuscaSimples(pContent, "Tel.: ", "</span>");


        return content;
    }
  • You know or don’t know how to do?

  • take a look at substring

  • First, why do you receive or capture the phones this way? This seems to me somewhat unusual.

2 answers

3

can use substring, the first digit is how many characters you want to jump from the string and the second is how many characters you want to pick from the first parameter.

string texto = "413232123441999993254";
var telefone1 = texto.Substring(0,10);
var telefone2 = texto.Substring(10, 11);

1

If landline and mobile phone numbers always come in the same order, the @Rafaelscheffer solution meets.

If they come sometimes in mixed order, do so:

public IEnumerable<string> Telefones(string pContent)
{
    ...

    var re = 
        new System.Text.RegularExpressions.Regex(@"[1-9]\d([0-8]\d{7}|9\d{8})");

    var matches = re.Matches(content);

    if (matches.Count > 0)
    {
        foreach (var match in matches)
        {
            yield return match.Value;
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.