字符串替换和高亮关键词的算法
2014-11-12 17:42:37 访问(2459) 赞(0) 踩(0)
-
通过字符串逐个比较和替换实现,没有用正则表达式,效率可能有点不好,但能简单实现要求:
/// <summary>
/// 关键词匹配的Class
/// </summary>
[Serializable]
public class KeyWordTextClass
{
/// <summary>
/// 关键词匹配的Class
/// </summary>
/// <param name="_IsKeyWord"></param>
/// <param name="_Text"></param>
public KeyWordTextClass(bool _IsKeyWord, string _Text)
{
m_IsKeyWord = _IsKeyWord;
m_Text = _Text;
}
/// <summary>
/// 内容
/// </summary>
protected string m_Text = "";
/// <summary>
/// 内容
/// </summary>
public string Text
{
get
{
return m_Text;
}
}
/// <summary>
/// 是否关键字
/// </summary>
protected bool m_IsKeyWord = false;
/// <summary>
/// 是否关键字
/// </summary>
public bool IsKeyWord
{
get
{
return m_IsKeyWord;
}
}
/// <summary>
/// 将字符串分割成列表数组
/// </summary>
/// <param name="TheName"></param>
/// <param name="KeyWord"></param>
/// <returns></returns>
public static List<KeyWordTextClass> BuildList
(
string TheName,
string KeyWord
)
{
if (TheName == null || TheName.Length == 0)
return null;
List<KeyWordTextClass> theResult = null;
if (KeyWord == null || KeyWord.Length == 0 || KeyWord.Trim().Length == 0)
{
theResult = new List<KeyWordTextClass>();
theResult.Add(new KeyWordTextClass(false, TheName));
return theResult;
}
string[] sArray = KeyWord.Split(new char[] { ' ' });
if (sArray == null || sArray.Length == 0)
{
theResult = new List<KeyWordTextClass>();
theResult.Add(new KeyWordTextClass(false, TheName));
return theResult;
}
int iLen = sArray.Length;
if (iLen == 1)
{
string strKeyWord = sArray[0];
if (strKeyWord == null)
{
theResult = new List<KeyWordTextClass>();
theResult.Add(new KeyWordTextClass(false, TheName));
return theResult;
}
strKeyWord = strKeyWord.Trim();
if (strKeyWord.Length == 0)
{
theResult = new List<KeyWordTextClass>();
theResult.Add(new KeyWordTextClass(false, TheName));
return theResult;
}
int idx = -1;
string lowerKeyWord = strKeyWord.ToLower();
string strTheName = TheName;
string lowerTheName = TheName.ToLower();
int keyWordLen = strKeyWord.Length;
theResult = new List<KeyWordTextClass>();
while (true)
{
idx = lowerTheName.IndexOf(lowerKeyWord);
if (idx == -1)
{
theResult.Add(new KeyWordTextClass(false, strTheName));
break;
}
if (idx != 0)
{
theResult.Add(new KeyWordTextClass(false, strTheName.Substring(0, idx)));
}
theResult.Add(new KeyWordTextClass(true, strTheName.Substring(idx, keyWordLen)));
strTheName = strTheName.Substring(idx + keyWordLen);
lowerTheName = lowerTheName.Substring(idx + keyWordLen);
}
return theResult;
}
else
{
bool isFirst = true;
List<KeyWordTextClass> resultList = null;
List<KeyWordTextClass> tmpList = null;
foreach (string s in sArray)
{
if (s == null || s.Trim().Length == 0)
continue;
if (isFirst)
{
theResult = BuildList(TheName, s);
isFirst = false;
}
else
{
resultList = new List<KeyWordTextClass>();
foreach (KeyWordTextClass ktc in theResult)
{
if (ktc == null)
continue;
if (ktc.IsKeyWord)
{
resultList.Add(ktc);
continue;
}
if (ktc.Text == null || ktc.Text.Length == 0)
continue;
tmpList = BuildList(ktc.Text, s);
if (tmpList != null)
{
foreach (KeyWordTextClass k in tmpList)
{
if (k == null)
continue;
resultList.Add(k);
}
}
}
theResult = resultList;
}
}
}
return theResult;
}
/// <summary>
/// 重组字符串,实现对关键词高亮
/// </summary>
/// <param name="theList"></param>
/// <param name="strColor"></param>
/// <returns></returns>
public static string BuildColorText(List<KeyWordTextClass> theList, string strColor)
{
if (theList == null || theList.Count == 0)
return "";
StringBuilder theResult = new StringBuilder();
if(strColor == null || strColor.Length == 0)
throw new Exception("strColor == null || strColor.Length == 0");
strColor = strColor.Replace("\"", """);
foreach (KeyWordTextClass ktc in theList)
{
if (ktc == null)
continue;
if (ktc.Text == null || ktc.Text.Length == 0)
continue;
if (ktc.IsKeyWord)
{
theResult.Append("<font color=\"" + strColor + "\">" + ktc.Text + "</font>");
}
else
{
theResult.Append(ktc.Text);
}
}
return theResult.ToString();
}
/// <summary>
/// 实现字符串替换和高亮关键词
/// </summary>
/// <param name="TheName"></param>
/// <param name="KeyWord"></param>
/// <param name="strColor"></param>
/// <returns></returns>
public static string ToColorText
(
string TheName,
string KeyWord,
string strColor
)
{
List<KeyWordTextClass> theList = BuildList(TheName, KeyWord);
return BuildColorText(theList, strColor);
}
}
标签:
字符串替换和高亮关键词的算法 


上一条:
下一条:
相关评论
发表评论