|
[code=csharp]/// <summary>
/// 截断字符串,末尾用appendString代替
/// </summary>
/// <param name="rawString">原字符串</param>
/// <param name="maxLength">长度</param>
/// <param name="appendString">被代替的字符</param>
/// <returns></returns>
public static string StrTrimSearch(string rawString, int maxLength, string appendString, string KeyWord)
{
if (string.IsNullOrEmpty(rawString) || (rawString.Length <= maxLength))
{
return rawString;
}
if (Encoding.Default.GetBytes(rawString).Length <= (maxLength * 2))
{
return rawString;
}
int length = Encoding.Default.GetBytes(appendString).Length;
StringBuilder builder = new StringBuilder();
int num3 = 0;
for (int i = 0; i < rawString.Length; i++)
{
char ch = rawString;
builder.Append(ch);
num3 += Encoding.Default.GetBytes(new char[] { ch }).Length;
if (num3 >= ((maxLength * 2) - length))
{
break;
}
}
if (!string.IsNullOrEmpty(KeyWord))
{
return (builder.ToString().Replace(KeyWord, "<span class=\"txt04\">" + KeyWord + "</span>") + appendString);
}
else
{
return (builder.ToString() + appendString);
}
}[/code]这个是我的方法,但是为什么 是有个别绑定的 字符串 关键字才会变色呢 |
|