17,740
社区成员
发帖
与我相关
我的任务
分享
比如数据库中有几条
<font style="color:red">大个大法官大个大法官的风格的风格的风格的风格大法官!</font>
<font style="color:yellow">大个大法大法官格的风格的风格大法官!</font>
<font style="color:blue">大多个个大法官的风格的风格的风格的风格大法官!</font>
<font style="color:red">大个大法官格大法官!</font>
我读取的时候,如果直接
public string getstr(string str)
{
if (str.Length>10)
{
return str.Substring(0,10) + "...";
}
else
{
return str;
}
}
然后,使用repeater绑定
<%#getstr(Eval("Title").ToString())%>
这样的话,读取出来,就不显示文字的颜色,而且,字符也显示了,,,怎么搞???才能显示文字,而且文字要对应相对应的颜色???
感谢各位!根据porschev这位帅哥,已经搞定,谢谢各位!
public static string getstr(string str)
{
if (str.Length > 10)
{
return str.Substring(0, 10) + "...";
}
else
{
return str;
}
}
public string getcolor(string str)
{
Regex re = new Regex(@"(<font [^>]+>)([^<]+)(</font>)", RegexOptions.None);
Match ma = re.Match(str);
int bb = ma.Length;
if (ma.Length > 0)
{
return str = ma.Groups[1] + getstr(ma.Groups[2].Value) + ma.Groups[3].Value;
}
else
{
return getstr(str);
}
}
<font style="color:red">大个大法官大个大法官的风格的风格的风格的风格大法官!</font>
<font style="color:red">大个大法大法官格的风格的风格大法官!</font>
<font style="color:blue">大多个个大法官的风格的风格的风格的风格大法官!</font>
<font style="color:yellow">大个大法官格大法官!</font>
大个大法官大个大法官的风格的风格的风格的风格大法官!
<font style="color:red">大个大法大法官格的风格的风格大法官!</font>
大多个个大法官的风格的风格的风格的风格大法官!
<font style="color:red">大个大法官格大法官!</font>
你这样是可以的,,如果没有样式呢??在怎么判断??
static void Main(string[] args)
{
string str = @"<font style=""color:red"">大个大法官大个大法官的风格的风格的风格的风格大法官!</font>";
Regex re = new Regex(@"(<font [^>]+>)([^<]+)(</font>)", RegexOptions.None);
Match ma = re.Match(str);
//string str = getstr(
str = ma.Groups[1] + getstr(ma.Groups[2].Value) + ma.Groups[3].Value;
Console.WriteLine(str);
Console.ReadLine();
}
public static string getstr(string str)
{
if (str.Length > 10)
{
return str.Substring(0, 10) + "...";
}
else
{
return str;
}
}
//str结果:<font style="color:red">大个大法官大个大法官...</font>
string tempStr = File.ReadAllText(@"C:\1.txt", Encoding.GetEncoding("GB2312"));
Regex re = new Regex(@"(?is)<font\s*style=""color:(?<color>(.*?))"">[^(font)]*</font>");
List<string> lists = new List<string>();
MatchCollection sr = re.Matches(tempStr);
foreach (Match me in sr)
{
str = "<font color='" +me.Groups["color"].Value + "'\">" + me.Groups["color"].Value+ "</font>"
lists.Add(str);//颜色,red在lists中
}
<font style="color:red">大个大法官大个大法官的风格的风格的风格的风格大法官!</font>
<font style="color:red">大个大法大法官格的风格的风格大法官!</font>
<font style="color:red">大多个个大法官的风格的风格的风格的风格大法官!</font>
<font style="color:red">大个大法官格大法官!</font>
string tempStr = File.ReadAllText(@"C:\1.txt", Encoding.GetEncoding("GB2312"));
Regex re = new Regex(@"(?is)<font\s*style=""color:(?<color>(.*?))"">[^(font)]*</font>");
List<string> lists = new List<string>();
MatchCollection sr = re.Matches(tempStr);
foreach (Match me in sr)
{
lists.Add(me.Groups["color"].Value);//颜色,red在lists中
}
string s = "<font style=\"color:red\">大个大法官大个大法官的风格的风格的风格的风格大法官!</font>";
Regex regex=new Regex(@"^<font (.*?)[^>]>(.*?)[^<]</font>$",RegexOptions.IgnoreCase);
Match m = regex.Match(s);
if (m.Groups.Count > 0)
{
string str = m.Groups[2].Value;
if (str.Length > 10)
{
str=str.Substring(0, 10) + "...";
}
str = "<font " + m.Groups[1].Value + "\">" + str + "</font>";
}