/// <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. [Artoolkit] Marker Training

    Link: Documentation About the Traditional Template Square Marker Limitations (重要) Traditional Templa ...

  2. 【zheng环境准备】安装activemq

    1.下载http://activemq.apache.org/activemq-5140-release.html 2.移动到linux机器上 3.解压 tar -zxvf apache-active ...

  3. [IOI 2000]POJ 1160 Post Office

    Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22278 Accepted: 12034 Descrip ...

  4. 跟bWAPP学WEB安全(PHP代码)--XPath注入

    XML/Xpath注入 看了下,A2里面是认证与会话管理的破坏或称之为绕过,没有特别要写的,很多就是小问题,可能会将这类问题放在最后写一下.一篇博客,这里还是更多的着重在能够获取信息或者服务器权限的漏 ...

  5. Unity3D Shader 空气扭动效果

    //预览图 //原理 一个摄像机CullingMask设置只可见"Distortion"的Layer(需要自己手动加),输入到一张RenderTexture,其实就是用于确定哪里要 ...

  6. ajax 获取服务器返回的XML字符串

    前台 解析失败不会抛出任何异常, 只会返回一个给定的错误文档 let l = console.log let http = ajanuw.create({ uri: 'http://localhost ...

  7. ElasticSearch入门 第六篇:复合数据类型——数组,对象和嵌套

    这是ElasticSearch 2.4 版本系列的第六篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  8. CSS3 transition 属性过渡效果 详解

    CSS3 transition 允许 CSS 元素的属性值在一定的时间区间内平滑地过渡.我们可以在不使用 Flash 动画或 JavaScript 的情况下,在元素从一种样式变换为另一种样式时为元素添 ...

  9. 怎样将flac音频格式转换成MP3格式

    Flac音频格式怎样转换成MP3格式呢?随着现在音频格式的不断多样性,生活中很多时候我们都会遇到音频格式转换的问题,如flac音频转MP3的问题,那么我们应该如何去解决这个问题呢?下面我们就一起去来一 ...

  10. /usr/bin/ld: 找不到 -lmsc----解决方案

    系统的默认搜索依赖库路径为,/usr/local/lib 在camkelists.txt文件中对可执行文件链接libmsc.so add_executable(iat_publish src/iat_ ...