String扩展 让你在PadLeft和PadRight时不再受单双字节问题困扰
/// <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时不再受单双字节问题困扰的更多相关文章
- JS中的PadLeft、PadRight,位数不足,自动补位,String扩展方法
类似C#中的 PadLeft.PadRight方法 //方法一 function FillZero(p) { return new Array(3 - (p + '').length + 1).joi ...
- C#小方法PadLeft 和 PadRight
1.在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位. PadLeft(int totalWidth, char paddingChar) //在字符串左边用 pad ...
- C#中用PadLeft、PadRight 补足位数
在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位. PadLeft(int totalWidth, char paddingChar) //在字符串左边用 paddi ...
- C# 中 PadLeft和PadRight 的用法
C# 中 PadLeft和PadRight 的用法 在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位. PadLeft(int totalWidth, char pa ...
- PadLeft 和 PadRight
1 PadLeft 即:向已知字符串左边补充字符,使整个字符串到达指定长度 CREATE FUNCTION PadLeft ( ),/*原始字符*/ @TotalLength int,/*总长度*/ ...
- sql函数PadLeft与PadRight代码实例
1.PadLeft函数向已知字符串左边补充字符,使整个字符串到达指定长度 CREATE FUNCTION PadLeft ( ),/*原始字符*/ @TotalLength int,/*总长度*/ ) ...
- 应用:计算字符串的长度(一个双字节字符长度计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] ...
- java——String、StringBuffer、StringBuilder、包装类、单双引号
String: String是一个特殊的类,被定义为final类型,为字符串常量,同样的字符串在常量池中不能重复. 但是由于使用关键字new创建新的字符串,java会在对中分配新的空间,这样即使字符串 ...
- C#中PadLeft和PadRight小知识点
当我们显示字符串数据时,有时候我们需要考虑数据的排列美观. 比如一些人名和一些编号,我们想让他们整齐对齐显示等. C# String类提供了2种操作方法: String.PadLeft(int tot ...
随机推荐
- [Tensorflow] Cookbook - Retraining Existing CNNs models - Inception Model
From: https://github.com/jcjohnson/cnn-benchmarks#alexnet 先大概了解模型,再看如果加载pre-training weight. 关于retai ...
- ios开发之--valueForKeyPath的用法
1.获取数组中的平均值,最大值,最小值,总和,代码如下: NSArray *ary = @[@,@,@,@,@,@,@]; [self caculateArray:ary]; -(NSString * ...
- E - 487--3279
Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is ...
- js的server worker创建子进程
类似nodejs的 child_process.fork() // index.html 主线程 function isClose(data){ if(data === 0) return true; ...
- ThinkPHP框架 系统规定的方法查询数据库内容!!同时也支持原生的SQL语句!
<?php namespace Admin\Controller; use Think\Controller; class MainController extends Controller{ ...
- Codeforces 835C - Star sky - [二维前缀和]
题目链接:http://codeforces.com/problemset/problem/835/C 题意: 在天空上划定一个直角坐标系,有 $n$ 颗星星,每颗星星都有坐标 $(x_i,y_i)$ ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 Dream hdu6440 Dream 给出一个(流氓)构造法
http://acm.hdu.edu.cn/showproblem.php?pid=6440 题意:让你重新定义任意一对数的乘法和加法结果(输出乘法口诀表和加法口诀表),使得m^p+n^p==(m+n ...
- vsftpd上传文件出现553 Could not create file错误解决方法
1.确定目录权限 2.关闭selinux
- git 将本地仓库提交至github
-or create a new repository on the command line touch README.md git init git add README.md git commi ...
- delphi 把数据库图片的存取
procedure TForm1.Button1Click(Sender: TObject); // 插入图片过程 var Stream:TMemoryStream;begin try Stream ...