Format a CPF string?

Asked

Viewed 25,170 times

10

I have a problem, in an app the user type the CPF, but only the numbers because it is very complicated to create mask on Windows Phone, and this CPF will be 'drawn' in a picture of a card, and to get more pleasant I need to draw it with its normal format, dotted. So how do I turn a string:

xxxxxxxxxxx

In:

xxx.xxx.xxx-xx

4 answers

28


I don’t know windows phone, but in pure c# would be like this:

public string teste(string cpf)
        {
            return Convert.ToUInt64(cpf).ToString(@"000\.000\.000\-00");
        }
  • 1

    worked perfectly, I was already thinking that I would have to keep cutting the string etc kk Thanks

  • 1

    Okay, good thing it worked. My concern would be windows phone, whether I would accept this type of mask or not.

  • Hello, Do you know why it looks like 0 in front of Cpf’s ? type 12345678910 is 0123.456.789-10

  • Perfect solution!

4

public string teste(string cpf)
{
    return Convert.ToUInt64(cpf).ToString(@"000\.000\.000\-00");
}

This solution does not work for those who have the CPF started with 2 zeros. To solve this problem, you can use this method:

//000.000.000-00
public string FormatCPF(string sender)
{
    string response = sender.Trim();
    if (response.Length == 11)
    {
        response = response.Insert(9, "-");
        response = response.Insert(6, ".");
        response = response.Insert(3, ".");
    }
    return response;
}
  • Ué, I tested with a CPF started by 0 and even then it worked, which was the CPF that you tested that was going wrong?

  • The problem is when it starts with 2 zeros

3

private void txtCNPJ_KeyPress(object sender, KeyPressEventArgs e)
        {
            ComboBox t = sender as ComboBox; // ou text Box
            if (e.KeyChar >= 48 && e.KeyChar <= 57)
            {
                t.SelectionStart = t.Text.Length + 1;

                if (t.Text.Length == 2 || t.Text.Length == 6)
                    t.Text += ".";
                else if (t.Text.Length == 10)
                    t.Text += "/";
                else if (t.Text.Length == 15)
                    t.Text += "-";
                t.SelectionStart = t.Text.Length + 1;
            }
        }

-3

private void maskedTextBox1_Leave(object sender, EventArgs e)
{
    // pode se usar codigo formata ou digitar diretamente na control maskTextBox so remover aspas e @
    maskedTextBox1.Mask = @"000\.000\.000\-00";
}

private void button1_Click(object sender, EventArgs e)
{
    //Criar string em branco
    string Salvarmask = "";

    //usar controle para gerar string
    Salvarmask = maskedTextBox1.Text;

    //para visualizar exemplo string Formatada
    label1.Text = Salvarmask.ToString();
}
  • Hello Marcio, could you provide an example of how to do this with the string that the author cited? Remember, he said "string," she’s the one who needs to be masked. I recommend [Edit] the answer and provide an example of how to edit such a mask.

  • The codes are not for windows Phone and C# and only example

Browser other questions tagged

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