52 lines
1.8 KiB
C#
52 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace PrintDemo
|
|
{
|
|
public class FileUtils
|
|
{
|
|
public static string getFixedWidthString(string strinput,int lenght)
|
|
{
|
|
return StringExtensions.PadRightWhileDouble(strinput, lenght, ' ');
|
|
}
|
|
}
|
|
public static class StringExtensions
|
|
{
|
|
/// <summary>
|
|
/// 按单字节字符串向左填充长度
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <param name="length"></param>
|
|
/// <param name="paddingChar"></param>
|
|
/// <returns></returns>
|
|
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))
|
|
{
|
|
return 0;
|
|
}
|
|
return Regex.Replace(input, @"[^\x00-\xff]", "aa").Length;//计算得到该字符串对应单字节字符串的长度
|
|
}
|
|
/// <summary>
|
|
/// 按单字节字符串向右填充长度
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <param name="length"></param>
|
|
/// <param name="paddingChar"></param>
|
|
/// <returns></returns>
|
|
public static string PadRightWhileDouble(this string input, int length, char paddingChar = '\0')
|
|
{
|
|
var singleLength = GetSingleLength(input);
|
|
return input.PadRight(length - singleLength + input.Length, paddingChar);
|
|
}
|
|
}
|
|
}
|