На C#
using System;
using System.Text;
using System.Text.RegularExpressions;
public class TextUtils
{
// ....
public static string Fading(string text)
{
if (text == null)
{
return text;
}
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0, length = text.Length; i < length; i++)
{
int opacity = (int)(100.0F / (length + 1) * (length - i));
stringBuilder.Append("<span style=\"opacity: 0.");
if (opacity < 10)
{
stringBuilder.Append("0").Append(opacity);
}
else
{
stringBuilder.Append(opacity % 10 == 0 ? opacity / 10 : opacity);
}
stringBuilder
.Append("; filter: progid:DXImageTransform.Microsoft.Alpha(opacity=")
.Append(opacity)
.Append("); zoom: 1;\">")
.Append(text[i])
.Append("</span>");
}
return stringBuilder.ToString();
}
public static string InLength(string text, int length, int fadingLength)
{
if (text == null || text.Length <= length || length < 2 || length <= fadingLength || fadingLength < 1)
{
return text;
}
Match match = Regex.Match(text, "\\S{" + (length + 1) + "}", RegexOptions.Compiled);
if (match.Success)
{
length += match.Index - fadingLength;
return text.Substring(0, length) + Fading(text.Substring(length, fadingLength));
}
else
{
return text;
}
}
// ....
}
Используется так:
Response.Write(TextUtils.InLength("01234567890123456789012345", 22, 10));
Варианта на javascript-е у меня нет, но думаю переделать не сложно.