根据用户asp.net c#的请求动态更改文本颜色 [英] dynamically change the color of text as requested by user asp.net c#
问题描述
我所拥有的是我将从.txt文件中读取的文本,并且该文本中出现的某些单词应该以不同的方式着色(这就像搜索具有特定错误率的单词一样)
如果用户希望在文本中找到AAA
:
ABA BBBBCGHTGHG AAA HHHGHGHGHGH GHA
然后我希望ABA被着色为例如红色,因为它有错误率= 1
AAA将是颜色为绿色,因为它的错误率为0
和GHA颜色为蓝色例如
文本和文字是未知的,直到由用户
我也希望有一个错误率颜色的复选框,所以用户检查红色和绿色只有错误率0和1的单词将在文本中着色,其余的将是黑色的,我希望它是动态的
我希望它很明显,谢谢你
我正在使用ASP.NET和C#
What I have is a text that I will be reading from a .txt file and certain words that appear in that text should be colored differently (it''s like searching for words with a certain error rate)
if the user wanted to find AAA
in text:
ABABBBBCGHTGHGAAAHHHGHGHGHGHGHA
then I want ABA to be colored for example red because it has error rate =1
AAA will be colored green because it has error rate of 0
and GHA colored in blue for instance
The text and words are unknown until defined by the user
and I would also would like to have like check boxes with the colors of error rate so it the user checked red and green only word with error rate 0 and 1 will be colored in the text and the rest will be in black ,, I want thid to be dynamically
I hope it''s clear thank you
I''m using ASP.NET and C#
推荐答案
你可以使用
You can use
string.replace("AAA", "<span style="color: rgb(255,0,0);">AAA</span>")
在构建文本时代码中的
。
in code behind when building your text.
您好,
试试此代码 - 使用正则表达式
Hi,
Try this Code - Using Regex
//Store orginal text in hdnSearchText.Value (HiddenField)
public string HighlightText(string argText)
{
string Search_Str = hdnSearchText.Value.ToString();
// Setup the regular expression and add the Or operator.
Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
// Highlight keywords by calling the
//delegate each time a keyword is found.
return RegExp.Replace(argText, new MatchEvaluator(ReplaceKeyWords));
}
public string ReplaceKeyWords(Match m)
{
return ("" + m.Value + "");
}
//Search Button Click Code
protected void btnSearch_Click(object sender,EventArgs e)
{
string arg = txtSearch.Text; // Get Search Term
string result = HighlightText(arg);
lblDisplay.Text = result; //Display result in any label or html editor
}
谢谢
Siva Rm K
Thanks
Siva Rm K
这篇关于根据用户asp.net c#的请求动态更改文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!