博 之 文

以 拼 搏 设 计 梦 想 , 以 恒 心 编 程 明 天
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

*参数说明:oldStr为原字符串,maxLength为截取长度,endWith为在末尾加入的字符

*/

public static string StringTruncat(string oldStr, int maxLength, string endWith)

{

if (string.IsNullOrEmpty(oldStr))//原字符串为空时仅返回endWith

{

return oldStr + endWith;

}

if (maxLength<1)//指定长度必须大于0

{

throw new Exception("返回的字符串长度必须大于0");

}

if (oldStr.Length>maxLength)//原字符串长度大于截取长度时进行处理

{

string strTmp = oldStr.Substring(0, maxLength);//对原字符串的子串进行处理

if (string.IsNullOrEmpty(endWith))//如果endWith为空返回子串

{

return strTmp;

}

else

return strTmp + endWith;//给子串加的末尾加上指定字符

}

return oldStr;//原字符串长度小于等于截取长度时不作处理