Format a CPF string?

6

I have a problem, in an app the user types the CPF, but only the numbers because it is very complicated to create a mask in Windows Phone, and that CPF will be 'drawn' in an image of a card, and to be more pleasant I need to draw it with its normal format with dots and dash. So how do I transform a string:

xxxxxxxxxxx

In:

xxx.xxx.xxx-xx
    
asked by anonymous 18.07.2015 / 23:37

2 answers

19

I do not know about windows phone, but in pure C # it would look like this:

public string teste(string cpf)
        {
            return Convert.ToUInt64(cpf).ToString(@"000\.000\.000\-00");
        }
    
19.07.2015 / 00:20
2
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;
            }
        }
    
21.07.2016 / 15:52