DataList显示字符串问题

Thr21ough 2011-03-27 08:35:36
我是用Word编辑器录入内容进数据库,读取数据的时候用DataList显示,但是只想截取128个字符,不过在截取的时候由于录入时使用了Word编辑器,有很多的格式字符串,我只想截取内容字符串,请问怎么处理。
...全文
100 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
636f6c696e 2011-04-02
  • 打赏
  • 举报
回复
用正则吧,是万能的...
IT0329 2011-04-02
  • 打赏
  • 举报
回复
写个方法将html的跟格式的都去除咯
wangting0613 2011-04-02
  • 打赏
  • 举报
回复
后台写个截取字符串内容的方法,,然后绑定的时候在前台调用,,,
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 tsapi 的回复:]
上面substring是硬方法,你有需要把格式去掉,下面用regx,你又需要一些固定的格式,除非你需要留下来的格式可以提出来,然后正则把它匹配起,否则无解
[/Quote]正确
skyroom1 2011-04-02
  • 打赏
  • 举报
回复
简单,
1,你需要写一个去掉字符中的html代码的方法,
2,然后在无html字符中,截取你想要多少个字符的方法。
/// <summary>
/// 去掉html字符
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
public static string NoHtml(string str)
{
Regex re = new Regex("<(.[^>]*)>");
str = re.Replace(str, "");
return str;
}


/// <summary>
/// 截取字符串
/// </summary>
/// <param name="inputString">字符串参数</param>
/// <param name="len">截取长度</param>
/// <param name="tail">超过长度自定义的尾巴</param>
/// <returns></returns>
public static string CutStr(string inputString, int len, string tail)
{
ASCIIEncoding ascii = new ASCIIEncoding();
int tempLen = 0;
string tempString = "";
byte[] s = ascii.GetBytes(inputString);
for (int i = 0; i < s.Length; i++)
{
if ((int)s[i] == 63)
{
tempLen += 2;
}
else
{
tempLen += 1;
}

try
{
tempString += inputString.Substring(i, 1);
}
catch
{
break;
}

if (tempLen > len)
break;
}
//如果截过则加上设置的尾巴
byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
if (mybyte.Length > len)
tempString += tail;
return tempString;
}
Daqing 2011-04-01
  • 打赏
  • 举报
回复
上面substring是硬方法,你有需要把格式去掉,下面用regx,你又需要一些固定的格式,除非你需要留下来的格式可以提出来,然后正则把它匹配起,否则无解
Thr21ough 2011-04-01
  • 打赏
  • 举报
回复
好吧,如果有高手解决此问题,马上结贴~
Thr21ough 2011-03-28
  • 打赏
  • 举报
回复
从数据库读取出来是有数据的,刚想着可以在存储的时候可以去掉格式,但是有的地方又需要格式。保存到数据库如果保留Word格式的话,截取字符串就不知道怎么只截取内容字符串?请高手指点!
bourbon1795 2011-03-27
  • 打赏
  • 举报
回复
刚刚写的有点问题 regex多余了
string s = @"<div align=""left""><b><span style=""color: red; font-size: 18pt"">一号轮防护打开</span></b></div><div align=""left""><b><span style=""color: lightgrey; font-size: 16pt"">1ST WHEEL GUARD OPEN</span></b></div><div align=""left""><span style=""font-size: 16pt"">     </span></div>";

MatchCollection mc = Regex.Matches(s, "(?<=>)[^<]+(?=<)");

foreach( var t in mc)
{
Console.WriteLine(t.ToString());
}
bourbon1795 2011-03-27
  • 打赏
  • 举报
回复
正则该下
MatchCollection mc = Regex.Matches(s, "(?<=>)[^<]+(?=<)");
bourbon1795 2011-03-27
  • 打赏
  • 举报
回复
string s = @"<div align=""left""><b><span style=""color: red; font-size: 18pt"">一号轮防护打开</span></b></div><div align=""left""><b><span style=""color: lightgrey; font-size: 16pt"">1ST WHEEL GUARD OPEN</span></b></div><div align=""left""><span style=""font-size: 16pt"">     </span></div>";
Regex r = new Regex(">[^<]+<");
MatchCollection mc = Regex.Matches( s, ">[^<]+<");

foreach( var t in mc)
{
Console.WriteLine(t.ToString());
}

Thr21ough 2011-03-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bourbon1795 的回复:]
正则行不
[/Quote]
能不能详细一些怎么用正则的?
比如下面是一个数据库的数据

<div align="left"><b><span style="color: red; font-size: 18pt">一号轮防护打开</span></b></div>
<div align="left"><b><span style="color: lightgrey; font-size: 16pt">1ST WHEEL GUARD OPEN</span></b></div>
<div align="left"><span style="font-size: 16pt">     </span></div>
<div align="left"><span style="font-size: 16pt">可按如下方法诊断:</span></div>
<div align="left"><span style="font-size: 16pt">        </span></div>
<div align="left"><span style="font-size: 15pt">1、检查一轮防护是否正常.</span></div>
<div align="left"><span style="font-size: 15pt">2、检查防护开关(2S212)是否接好.</span></div>
<div align="left"><span style="font-size: 15pt">3、检查10号板的30脚是否有输入。</span></div>
<div align="left"><span style="font-size: 15pt">4、相关图纸编号:</span><span style="font-size: 16pt">http://</span><span style="font-size: 15pt">1207001</span></div>

我只想截取格式之外的内容字符串
Thr21ough 2011-03-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fangxinggood 的回复:]
在查出来结果中用 SubString(0, 128); 不可以吗?
[/Quote]

这样的话,截取的128字符串也报过格式字符串了,想只截取内容字符串。
机器人 2011-03-27
  • 打赏
  • 举报
回复
在查出来结果中用 SubString(0, 128); 不可以吗?

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧