Format String in C #

Source: Internet
Author: User
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. text;
  4. Namespace string
  5. {
  6. Class Format String
  7. {
  8. Public static void main ()
  9. {
  10. // The console. writeline method is actually calling string. Format (static), and the parameters are infinite.
  11. // In this way, the writeline method does not actually perform a specific formatting operation.
  12. // In the string. Format method, replace the first format specifier.
  13. // In this example, stringbuilder is used for operations.
  14. // In this replacement, stringbuilder uses static files for initialization, that is, the fixed part.
  15. // Then call stringbuilder. appendformat () to pass the first format specifier example "{600: e}" in pairs with the corresponding first object, for example:
  16. // This loop continues until all format characters are fully formatted.
  17. // The following code is custom formatting.
  18. Person = new person ();
  19. Person. Age = 22;
  20. Person. Name = "Aladdin ";
  21. Console. writeline ("Format: {0,400: c}", person );
  22. Console. Read ();
  23. }
  24. }
  25. Class person: iformattable
  26. {
  27. Public string name;
  28. Public int age;
  29. // Method for implementing the formatting Interface
  30. Public String tostring (string format, iformatprovider formatprovider)
  31. {
  32. String flag = NULL;
  33. If ("E". Equals (Format ))
  34. {
  35. Flag = "E:" + this. Name + "" + this. Age;
  36. }
  37. Else if ("A". Equals (Format ))
  38. {
  39. Flag = "A:" + this. Name + "" + this. Age;
  40. }
  41. Else
  42. {
  43. Flag = "no. You can format a fart! ";
  44. }
  45. Return flag;
  46. }
  47. // Override the tostring method of the object
  48. Public override string tostring ()
  49. {
  50. Return "Object:" + this. Name + "" + this. Age;
  51. }
  52. }
  53. }

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.