Drip Supplements-Custom Format control String.Format behavior

Source: Internet
Author: User

Click to download the sample code

String.Format the signature of an overloaded method is as follows

1  Public Static string format (2    IFormatProvider provider,3     string  format,  4     params  object[] args5 )

You can control specific behavior during String.Format execution by customizing the IFormatProvider interface.

The process is as follows

1, custom class implementation  iformatprovider interface (object GetFormat method)

1  Public Object GetFormat (Type formattype) 2 {3     if typeof (ICustomFormatter)) 4         return  This ; 5     Else 6         return NULL ; 7 }

2, implement ICustomFormatter excuse (string Format method)

Excerpt from MSDN. The function is a string output that controls the Int64 type.

1  Public stringFormat (stringFmtObjectArg, IFormatProvider formatprovider)2 {3     //provide default formatting if Arg is a Int64.4     if(Arg. GetType ()! =typeof(Int64))5         Try6         {7             returnhandleotherformats (FMT, arg);8         }9         Catch(FormatException e)Ten         { One             Throw NewFormatException (String.Format ("The format of ' {0} ' is invalid.", FMT), e); A         } -  -     //provide default formatting for unsupported format strings. the     stringUFMT =FMT. ToUpper (CultureInfo.InvariantCulture); -     if(! (Ufmt = ="H"|| UFMT = ="I")) -         Try -         { +             returnhandleotherformats (FMT, arg); -         } +         Catch(FormatException e) A         { at             Throw NewFormatException (String.Format ("The format of ' {0} ' is invalid.", FMT), e); -         } -  -     //Convert argument to a string. -     stringresult =Arg. ToString (); -  in     //If account number was less than characters, pad with leading zeroes. -     if(result.) Length <acct_length) toresult = result. PadLeft (Acct_length,'0'); +     //If account number was more than characters, truncate to characters. -     if(result.) Length >acct_length) theresult = result. Substring (0, acct_length); *  $     if(Ufmt = ="I")//integer-only format. Panax Notoginseng         returnresult; -     //Add hyphens for H format specifier. the     Else                                          //hyphenated format. +         returnResult. Substring (0,5) +"-"+ result. Substring (5,3) +"-"+ result. Substring (8); A}

Using the well-implemented Format class

1 LongAcctnumber;2 Doublebalance;3 DaysOfWeek wday;4 stringoutput;5 6Acctnumber =104254567890;7Balance =16.34;8Wday =Daysofweek.monday;9 TenOutput = String.Format ((NewAcctNumberFormat (), One                        "On {2}, the balance of account {0:h} is {1:C2}.", A Acctnumber, balance, wday); -Console.WriteLine (output);

Execution results are as follows

Resources

Culture invariant Decimal.tryparse () http://stackoverflow.com/questions/23131414/culture-invariant-decimal-tryparse

What does IFormatProvider do? Http://stackoverflow.com/questions/506676/what-does-iformatprovider-do

IFormatProvider Interface https://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx

Drip Supplements-Custom Format control String.Format behavior

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.