Alignment of string. Format

Source: Internet
Author: User
// Alignment

Console. writeline ("{0,-20} {1}", "fafdaafd:", "213 ");
Console. writeline ("{0,-20} {1}", "fdfaa:", "13 ");
// Add an English letter T, which can be aligned
Console. writeline ("{0,-20} {1}", "fafdatafd:", "213 ");
Console. writeline ("{0,-20} {1}", "fdfaa:", "13 ");
 
// Add one Chinese character to reduce the number of two letters, which can be aligned
Console. writeline ("{0,-20} {1}", "fafdfd my Ben:", "213 ");
Console. writeline ("{0,-20} {1}", "fdfaa:", "13"); // Add a Chinese character to reduce one letter, which cannot be aligned
Console. writeline ("{0,-20} {1}", "fafdaafd my runner:", "213 ");
Console. writeline ("{0,-20} {1}", "fdfaa:", "13 ");
The following result is displayed in the message box: Solution:
Public class qstring  
{
String S;
/// <Summary>
/// Create an instance
/// </Summary>
/// <Param name = "S"> </param>
Public qstring (string S)
{
This. S = s;
}  
/// <Summary>
/// To be formatted
/// </Summary>
/// <Param name = "length"> length </param>
/// <Param name = "CHR"> complement </param>
/// <Returns> formatted data </returns>
/// <Remarks>
/// Based on English letters, the maximum length and actual length are used to calculate the complement length.
/// For example, C 20
/// C I 20-1
/// C multiple me 20-3
/// </Remarks>
Public String padleft (INT length, char CHR)
{
Return S. padleft (length-new system. Text. regularexpressions. RegEx (@ "[\ u4e00-\ u9fa5]"). Matches (s). Count, CHR );}
/// <Summary>
/// To be formatted
/// </Summary>
/// <Param name = "length"> length </param>
/// <Param name = "CHR"> complement </param>
/// <Returns> formatted data </returns>
/// <Remarks>
/// Based on English letters, the maximum length and actual length are used to calculate the complement length.
/// For example, C 20
/// C I 20-1
/// C multiple me 20-3
/// </Remarks>
Public String padright (INT length, char CHR)
{
Return S. padright (length-new system. Text. regularexpressions. RegEx (@ "[\ u4e00-\ u9fa5]"). Matches (s). Count, CHR );}
} Msgbox is not applicable to a large number of practical applications that can be used in many cases. Using system;
Using system. Collections. Generic;
Using system. Text; namespace qoushui. dll
{
Public class qstring  
{
String S;
/// <Summary>
/// Create an instance
/// </Summary>
/// <Param name = "S"> </param>
Public qstring (string S)
{
This. S = s;
Hcount = reg. Matches (s). count;
} System. Text. regularexpressions. RegEx Reg = new system. Text. regularexpressions. RegEx (@ "[\ uff00-\ Uffff \ u4e00-\ u9fa5]");
Int hcount = 0; // <summary>
/// To be formatted
/// </Summary>
/// <Param name = "length"> length </param>
/// <Param name = "CHR"> complement </param>
/// <Returns> formatted data </returns>
/// <Remarks>
/// Based on English letters, the maximum length and actual length are used to calculate the complement length.
/// For example, C 20
/// C I 20-1
/// C multiple me 20-3
/// </Remarks>
Public String padleft (INT length, char CHR)
{
If (length-S. Length-Reg. Matches (s). Count <0)
Length = S. Length + Reg. Matches (s). count;  
Return S + new string (CHR, length-S. Length-Reg. Matches (s). Count );
}
/// <Summary>
/// To be formatted
/// </Summary>
/// <Param name = "length"> length </param>
/// <Param name = "CHR"> complement </param>
/// <Returns> formatted data </returns>
/// <Remarks>
/// Based on English letters, the maximum length and actual length are used to calculate the complement length.
/// For example, C 20
/// C I 20-1
/// C multiple me 20-3
/// </Remarks>
Public String padright (INT length, char CHR)
{
If (length-S. Length-Reg. Matches (s). Count <0)
Length = S. Length + Reg. Matches (s). count;  
Return new string (CHR, length-S. Length-Reg. Matches (s). Count) + S;  
}
}
}

Publish by note

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.