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. ThatX
is a number or is the literalX
?– Leonel Sanches da Silva
Well we’re talking about rg, some have the literal character digit, in this case X.
– Mauricio Ferraz
Solved +/- :) Check this out: http://answall.com/a/30168/101 Do not capture
Exception
mainly just to relaunch it. Take thesetry catch
that they are helping zero and making debugging difficult if given any exception. And there really is reason for the data to come asobject
? There’s no other way to fix this?– Maniero
@Gypsy
String.Format
is suitable for numbers only? What is the problem of using with strings, for example?– Richard Dias
The data that comes from the paramenters comes from the grid through this Templatefield so I use Object. <asp:TemplateField HeaderText="Cpf">
 <ItemTemplate>
 <asp:Label ID="lblCpf" runat="server" Text='<%# this.CpfFormat(DataBinder.Eval(Container,"DataItem.Cpf"))%>' /> </Itemtemplate> </Asp:Templatefield>
– Mauricio Ferraz
@Richarddias The problem is to apply a mask formatting. Until today, I could only make it work for numerical variables (
int
,float
,long
,double
).– Leonel Sanches da Silva