///
/// 指定长度字符串 不足补空格
///
/// 字符串
/// 长度
///
public static String formatTxt(String src, int len)
{
return formatTxt(src, len, " ", true);
}
///
/// 指定长度字符串 不足后面补充字符串
///
/// 字符串
/// 长度
/// 补充字符串
///
public static String formatTxt(String src, int len, String addC)
{
return formatTxt(src, len, addC, true);
}
///
/// 获取定长随机数
///
///
///
public static String getRandom(int len)
{
int max = (int)Math.Pow(10.0D, len + 1) - 1;
Random random = new Random();
return formatInt(random.Next(max), len);
}
///
/// 指定长度 int 不足前面补充0
///
///
///
///
public static String formatInt(int num, int len)
{
String str = num.ToString();
return formatTxt(str, len, "0", false);
}
///
/// 获取定长的字符串
///
/// 字符串
/// 长度
///
public static String getSubString(String str, int len)
{
byte[] bytes = ByteUtil.getBytes(str);
if (bytes.Length > len)
{
byte[] copy = new byte[len];
Array.Copy(bytes, copy, len);
return getString(copy);
}
return str;
}
///
/// 指定长度字符串 不足补充字符串
///
/// 字符串
/// 长度
/// 补充字符串
/// true 后面补充
///
public static String formatTxt(String src, int len, String addC, bool isAppend)
{
if (src == null)
src = "";
String str = src;
int dif_len = len - ByteUtil.getLength(str);
for (int i = 0; i
if (isAppend)
str = str + addC;
else
{
str = addC + str;
}
}
if (Encoding.Default.GetBytes(str).Length > len)
str = getSubString(str, len);
return str;
}
///
/// byte[]转换成String
///
///
///
public static String getString(byte[] bytes)
{
return System.Text.Encoding.Default.GetString(bytes);
}