Format Strings to RG

Asked

Viewed 1,828 times

5

How can I format this String: 12345678x for this: 12,345,678-X ? I tried to use the String.Format but I couldn’t.

Solved:

public string RgFormat(object rg)
{   
    string strRg = rg.ToString();
    return strRg.Substring(0, 2) + "." + strRg.Substring(2, 3) + "." + strRg.Substring(5, 3) + "-" + strRg.Substring(8, 1).ToUpper();             
}

public string CpfFormat(object cpf)
{         
    string strCpf = cpf.ToString();
    return strCpf.Substring(0, 3) + "." + strCpf.Substring(3, 3) + "." +      strCpf.Substring(6, 3) + "-" + strCpf.Substring(9, 2).ToUpper();    
}
  • String.Format is suitable only for numbers. That X is a number or is the literal X?

  • Well we’re talking about rg, some have the literal character digit, in this case X.

  • Solved +/- :) Check this out: http://answall.com/a/30168/101 Do not capture Exception mainly just to relaunch it. Take these try catch that they are helping zero and making debugging difficult if given any exception. And there really is reason for the data to come as object? There’s no other way to fix this?

  • @Gypsy String.Format is suitable for numbers only? What is the problem of using with strings, for example?

  • The data that comes from the paramenters comes from the grid through this Templatefield so I use Object. <asp:TemplateField HeaderText="Cpf">&#xA; <ItemTemplate>&#xA; <asp:Label ID="lblCpf" runat="server" Text='<%# this.CpfFormat(DataBinder.Eval(Container,"DataItem.Cpf"))%>' /> </Itemtemplate> </Asp:Templatefield>

  • @Richarddias The problem is to apply a mask formatting. Until today, I could only make it work for numerical variables (int, float, long, double).

Show 1 more comment

3 answers

6


I cannot see a better solution than this. I thought to use ToString() or string.Format() but I didn’t find an easy solution (maybe you have:

using static System.Console;

public class Program {
    public static void Main() {
        WriteLine(FormataRG("12345678x"));
        WriteLine(FormataRG("123456789"));
    }
    public static string FormataRG(string texto) => texto.Substring(0, 2) + "." + texto.Substring(2, 3) + "." + texto.Substring(5, 3) + "-" + texto.Substring(8, 1).ToUpper();
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Note that if it is RG itself, each can have a different format then this is not a universal solution. You have to identify the format first. Validation would already be another operation.

  • Well the result is what I need, I thought I had some way to do with Tostring or String.Format(), the Cpf and Phone I did with Tostring().

  • But thanks I’ll use your code.

  • It may exist but for all I know and I thought, I couldn’t find anything that fits. What surprised me. If you look hard, you may have a way, but if you’re less cool than this, it won’t do you any good.

2

An alternative to the @Maniero response is to use Insert.

using System;

public class Program
 {
    public static void Main()
    {
        Console.WriteLine(mask("12345678x"));
    }

    public static string mask(string numbers) {
        return numbers.Insert(2, ".").Insert(6, ".").Insert(10, "-");
    }
 }

See working on @NET Fiddle.

0

Try it this way

Text1.Text = Text1.Text.Replace("X", "")

double numero;
if (Double.TryParse(Text1.Text, out numero))
      Text1.Text = numero.ToString(@"000\.000\.000\-X");
else
      MessageBox.Show("Valor inválido: " + Text1.Text);
  • I tested your code more have problem when I receive a rg only with example numbers: 12.123.123-1, It puts the X at the end, maybe I have not made clear in the question what really needed, anyway thank you.

  • But it is not to format with X at the end ?

  • Yes but sometimes it comes rg without X with numeric digit.

Browser other questions tagged

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