130xxxxxxxx,960xxxx ,人工服务 ,20040405,081223,63 ,2 ,2.00 ,4.00 ,鸿联 ,
函数的功能是在右边增加已知个数的空格,我自己写的不知道什么原因,是汉字的就能加上,是数字型的加不上求高手帮忙下面是我自己写的
public string FillBlank(string strin ,int len)
{
byte[] streamBuff = null;
streamBuff = System.Text.ASCIIEncoding.Default.GetBytes(strin) ;
int flen;
if(len>streamBuff.Length)
{
flen=len-streamBuff.Length;
strin=strin.PadRight(flen,' ');
return strin;
}
return strin;
}
函数的功能是在右边增加已知个数的空格,我自己写的不知道什么原因,是汉字的就能加上,是数字型的加不上求高手帮忙下面是我自己写的
public string FillBlank(string strin ,int len)
{
byte[] streamBuff = null;
streamBuff = System.Text.ASCIIEncoding.Default.GetBytes(strin) ;
int flen;
if(len>streamBuff.Length)
{
flen=len-streamBuff.Length;
strin=strin.PadRight(flen,' ');
return strin;
}
return strin;
}
{
string s="陈12阿ab";
richTextBox1.Text=FillBlank(s,10);
} private string FillBlank(string strin ,int len)
{
int byteCount=System.Text.Encoding.Default.GetByteCount(strin);
if(byteCount<len)
{
strin+=new string(' ',len-byteCount);
}
return strin;
}
不要使用PadRight,PadRight是添加unicode字符,一个空格字符也当两个字节来算。
//运行这段代码就知道结果,
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Configuration;
using Microsoft.Data.SqlXml;using Zhzuo;
namespace Zhzuo.ConsoleTest
{
public class ZZConsole
{
[STAThread]
static void Main(string[] args)
{
string oldString ="函A测b";
Console.WriteLine(String.Format("字符串:[{0}] / byte长度:[{1}]",oldString,GetByteLenth(oldString)));//6
string formatedString = FillBlank(oldString , 9);
Console.WriteLine(String.Format("字符串:[{0}] / byte长度:[{1}]",formatedString,GetByteLenth(formatedString)));//9 formatedString = FillBlank1(oldString , 9);
Console.WriteLine(String.Format("字符串:[{0}] / byte长度:[{1}]",formatedString,GetByteLenth(formatedString)));//9
Console.ReadLine();
} public static int GetByteLenth(string str)
{
byte[] bs = System.Text.Encoding.Default.GetBytes(str);
return bs.Length;
}
//len以字节为单位
public static string FillBlank(string strin ,int len)
{
byte[] streamBuff = null;
streamBuff = System.Text.Encoding.Default.GetBytes(strin) ; if(len>streamBuff.Length)
{
//int flen;//如果需要这个变量放在这里更好
//flen=len-streamBuff.Length;
//strin=strin.PadRight(flen,' ');flen参数表示总长度,而不是差量
byte[] newdata = new byte[len];
Array.Copy(streamBuff,0,newdata,0,streamBuff.Length);
return System.Text.Encoding.Default.GetString(newdata);
}
return strin;
} //len以字符为单位
public static string FillBlank1(string strin,int len)
{
if(len>strin.Length)
{
return strin.PadRight(len,' ');
}
return strin;
}
}
}