/// <summary>
/// 按单字节字符串向左填充长度
/// </summary>
/// <param name="input"></param>
/// <param name="length"></param>
/// <param name="paddingChar"></param>
/// <returns></returns>
public static string PadLeft2(this string input, int length, char paddingChar = '\0')
{
return Pad(input, length, paddingChar, true);
}
/// <summary>
/// 按单字节字符串向右填充长度
/// </summary>
/// <param name="input">输入字符串</param>
/// <param name="length">单字节长度</param>
/// <param name="paddingChar">补位字符</param>
/// <returns></returns>
public static string PadRight2(this string input, int length, char paddingChar = '\0')
{
return Pad( input, length, paddingChar, false);
} private static string Pad(string input, int length, char paddingChar,bool isLeft)
{
var isDoubleChar = Regex.IsMatch(paddingChar.ToString(), @"[^\x00-\xff]");
var singleLength = Regex.Replace(input, @"[^\x00-\xff]", "aa").Length;
var num = (length - singleLength) *1.0/(isDoubleChar?:);
var newStr=new string(paddingChar,(int)num);
if (isLeft)
{
input = newStr + input;
}
else
{
input = input + newStr;
}
return input;
}

String扩展 让你在PadLeft和PadRight时不再受单双字节问题困扰的更多相关文章

  1. JS中的PadLeft、PadRight,位数不足,自动补位,String扩展方法

    类似C#中的 PadLeft.PadRight方法 //方法一 function FillZero(p) { return new Array(3 - (p + '').length + 1).joi ...

  2. C#小方法PadLeft 和 PadRight

    1.在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位. PadLeft(int totalWidth, char paddingChar) //在字符串左边用 pad ...

  3. C#中用PadLeft、PadRight 补足位数

    在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位. PadLeft(int totalWidth, char paddingChar) //在字符串左边用 paddi ...

  4. C# 中 PadLeft和PadRight 的用法

    C# 中 PadLeft和PadRight 的用法 在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位. PadLeft(int totalWidth, char pa ...

  5. PadLeft 和 PadRight

    1 PadLeft 即:向已知字符串左边补充字符,使整个字符串到达指定长度 CREATE FUNCTION PadLeft ( ),/*原始字符*/ @TotalLength int,/*总长度*/ ...

  6. sql函数PadLeft与PadRight代码实例

    1.PadLeft函数向已知字符串左边补充字符,使整个字符串到达指定长度 CREATE FUNCTION PadLeft ( ),/*原始字符*/ @TotalLength int,/*总长度*/ ) ...

  7. 应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) String.prototype.len=function(){return this.replace(/[^\x00-\xff]/g,"aa").length;}

    应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) String.prototype.len=function(){return this.replace(/[^\x00-\xff] ...

  8. java——String、StringBuffer、StringBuilder、包装类、单双引号

    String: String是一个特殊的类,被定义为final类型,为字符串常量,同样的字符串在常量池中不能重复. 但是由于使用关键字new创建新的字符串,java会在对中分配新的空间,这样即使字符串 ...

  9. C#中PadLeft和PadRight小知识点

    当我们显示字符串数据时,有时候我们需要考虑数据的排列美观. 比如一些人名和一些编号,我们想让他们整齐对齐显示等. C# String类提供了2种操作方法: String.PadLeft(int tot ...

随机推荐

  1. Android样式的开发:shape篇

    转载请注明:转载自Keegan小钢并标明原文链接:http://keeganlee.me/post/android/20150830微信订阅号:keeganlee_me写于2015-08-30 And ...

  2. WCF ChannelFactory<T> WCF Channel and ChannelFactory Caching

    https://stackoverflow.com/questions/3200197/creating-wcf-channelfactoryt?rq=1 https://stackoverflow. ...

  3. JS 使用html2canvas实现截图功能的问题记录和解决方案

    在实现“截图”功能时,遇到几个bug,研究了一个上午,终于全部解决了: 下面给大家分享下: 1."图片资源跨域",导致无法截图. 浏览器会提示下面的错误 DOMException: ...

  4. 除了/etc/init.d/加启动脚本 或者在/etc/rc.local中加启动命令,还可以通过crontab来完成服务器重启后自动启动服务的操作

    @reboot /bin/sh /opt/soft/percona/bin/mysqld_safe --defaults-file=/mnt/perconadata/my.cnf --basedir= ...

  5. linux 几个命令

    tail: tail -20 xxx  --查看xxx文件的最后20行 more:分页查看,只能向后,不能向前 less:查看文件,可向前,向后,用的比较多 ll -h|more:当文件较多时,可以通 ...

  6. 十、K3 WISE 开发插件《SQL Profiler跟踪单据操作时产生的SQL语句》

    =================================== 目录: 1.查询帐套的数据库DBID 2.配置需要跟踪数据库的DBID 3.配置跟踪参数 4.跟踪进行 5.分析跟踪语句 === ...

  7. VBA字符串处理大全

    https://blog.csdn.net/goldengod/article/details/73558537  VBA字符串处理大全-from EH http://club.excelhome.n ...

  8. 如何获取类或属性的自定义特性(Attribute)

    如何获取类或属性的自定义特性(Attribute) 问题说明: 在ActiveRecord或者其他的ORM等代码中, 我们经常可以看到自定义特性(Attribute)的存在(如下面的代码所示) [Pr ...

  9. day_6.20动态加载py文件

    __import__() 魔法方法! 关于动态网站打开的  代码流程!

  10. A - Cable master

    Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Commi ...