String extension makes you no longer suffer from single-and double-byte problems in PadLeft and PadRight.

Source: Internet
Author: User

String extension makes you no longer suffer from single-and double-byte problems in PadLeft and PadRight.

In NET, the Length of a string does not distinguish whether the character contained in the current string is a single or dubyte character, as a result, when we use the PadLeft and PadRight methods of strings to process some strings that require typographical display, the related display will be uneven, so here we have such an extension, let the double byte display occupy two single byte width "vomit", the following is the specific code

 

Using System. Text. RegularExpressions; public static class StringExtensions {////// Fill the length to the left by a single-byte string ///////////////
 Public static string PadLeftWhileDouble (this string input, int length, char paddingChar = '\ 0') {var singleLength = GetSingleLength (input); return input. padLeft (length-singleLength + input. length, paddingChar);} private static int GetSingleLength (string input) {if (string. isNullOrEmpty (input) {throw new ArgumentNullException ();} return Regex. replace (input, @ "[^ \ x00-\ xff]", "aa "). length; // calculate the Length of a single-byte string }////// Fill the length to the right based on a single-byte string ///////////////
 Public static string PadRightWhileDouble (this string input, int length, char paddingChar = '\ 0') {var singleLength = GetSingleLength (input); return input. padRight (length-singleLength + input. length, paddingChar );}}
The test code is as follows:

 

 

Console. writeLine ("123 test ". padRightWhileDouble (25) + "1"); Console. writeLine ("123 test ". padRightWhileDouble (25, 'A') + "1"); Console. writeLine ("1231212 ". padRightWhileDouble (25) + "1"); Console. writeLine ("1231212 ". padRightWhileDouble (25, 'A') + "1"); Console. writeLine ("123 test ". padLeftWhileDouble (25) + "1"); Console. writeLine ("123 test ". padLeftWhileDouble (25, 'A') + "1"); Console. writeLine ("1231212 ". padLeftWhileDouble (25) + "1"); Console. writeLine ("1231212 ". padLeftWhileDouble (25, 'A') + "1 ");
Test

 


 

Related Article

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.