截取字符串的问题 现在我要截取字符串 <font color= "red "> 这里是内容 </font>怎么才能去只截取内容 而不截取格式呢 就算不保留格式也行 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 后台写个截取方法 public string getSubString(string title, int strlength) { if (stringToSub == null || stringToSub.Length == 0 || length <= 0) return ""; Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled); char[] stringChar = stringToSub.ToCharArray(); StringBuilder sb = new StringBuilder(); int nLength = 0; bool isCut = false; for (int i = 0; i < stringChar.Length; i++) { if (regex.IsMatch((stringChar[i]).ToString())) { sb.Append(stringChar[i]); nLength += 2; } else { sb.Append(stringChar[i]); nLength = nLength + 1; } if (nLength > length) { isCut = true; break; } } if (isCut) return sb.ToString() + "..."; else return sb.ToString(); }这是我的方法,可能实际开发中LZ不需要写这么复杂,直接判断长度,如果长度超出截取就可以了<%#getSubString("这里是内容",40)%> 用js弄就很简单了!<div id="aa"><font color= "red "> 这里是内容 </font></div>document.getElementById("aa").innerText <font color= "red "> 这里是内容 </font>开始截取的位置是第一次发现“>”,结束位置是第二次发现“<” <div id="aa"><font color= "red "> 这里是内容 </font></div>document.getElementById("aa").innerTextup 很简单。用jquery一句话.<font color= "red "> 这里是内容 </font><script src="App_common/scripts/jquery-1.4.2.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function(){ $("font[color='red']").val();//输出结果:这里是内容 }); </script> 我这些字符串是放在datalist里面的还有就是我想在后台截取 先去标签 再截str = Regex.Replace(str, @"<[^<>]*>", "");str = str.SubString(...); <input id="hidvar" type="hidd***" ruant="server">var Varfont=$("font[color=red]").val();$("#hidvar").val(Varfont);后台:string ss=this.hidvar.value; public string getSubString(string title, int strlength) { if (stringToSub == null || stringToSub.Length == 0 || length <= 0) return ""; Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled); char[] stringChar = stringToSub.ToCharArray(); StringBuilder sb = new StringBuilder(); int nLength = 0; bool isCut = false; for (int i = 0; i < stringChar.Length; i++) { if (regex.IsMatch((stringChar[i]).ToString())) { sb.Append(stringChar[i]); nLength += 2; } else { sb.Append(stringChar[i]); nLength = nLength + 1; } 屏蔽 html标签 调用一下就好了 祝你好运! public static string NoHTML(string Htmlstring) { string strOutput = Htmlstring; strOutput = Regex.Replace(strOutput, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase); // strOutput = Regex.Replace(strOutput,@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>","",RegexOptions.IgnoreCase);//在个别情况下会引起IIS吃爆CPU的现象 strOutput = Regex.Replace(strOutput, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase); strOutput = Regex.Replace(strOutput, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase); strOutput = Regex.Replace(strOutput, @"-->", "", RegexOptions.IgnoreCase); strOutput = Regex.Replace(strOutput, @"<!--.*", "", RegexOptions.IgnoreCase); strOutput = Regex.Replace(strOutput, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase); strOutput = Regex.Replace(strOutput, @"&(amp|#38);", "&", RegexOptions.IgnoreCase); strOutput = Regex.Replace(strOutput, @"&(lt|#60);", "<", RegexOptions.IgnoreCase); strOutput = Regex.Replace(strOutput, @"&(gt|#62);", ">", RegexOptions.IgnoreCase); strOutput = Regex.Replace(strOutput, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase); strOutput = Regex.Replace(strOutput, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase); strOutput = Regex.Replace(strOutput, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase); strOutput = Regex.Replace(strOutput, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase); strOutput = Regex.Replace(strOutput, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase); strOutput = Regex.Replace(strOutput, @"&#(\d+);", "", RegexOptions.IgnoreCase); strOutput.Replace("<", ""); strOutput.Replace(">", ""); strOutput.Replace("\r\n", ""); strOutput = HttpContext.Current.Server.HtmlEncode(strOutput).Trim(); strOutput=strOutput.Replace(" ", ""); return strOutput; } Array赋值 当把一个txtbox空间的mode属性设为密码的时候,如何得到它里边的值呢? 大家谈谈用nhibernate、强类型的dataset和类petshop式做数据层有何优缺点 关于选择的问题 打开IIS,提示连接不到服务器 DataGrid分页格式控制问题 求救出问题了,帮帮忙各位 请教各位大虾:如何在按钮控件中实现HyperLink控件的“在新窗口中打开页面的”功能? 求救!!关于水晶报表打印的错误。急!!! 新版太无语勒,看到回复数有16了,我这里只有6,根本看不到下面的信息 请问sitemap站点地图的网页公用了。该怎么办 请问这两句话的区别是什么
{
if (stringToSub == null || stringToSub.Length == 0 || length <= 0) return "";
Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
char[] stringChar = stringToSub.ToCharArray();
StringBuilder sb = new StringBuilder();
int nLength = 0;
bool isCut = false;
for (int i = 0; i < stringChar.Length; i++)
{
if (regex.IsMatch((stringChar[i]).ToString()))
{
sb.Append(stringChar[i]);
nLength += 2;
}
else
{
sb.Append(stringChar[i]);
nLength = nLength + 1;
} if (nLength > length)
{
isCut = true;
break;
}
}
if (isCut)
return sb.ToString() + "...";
else
return sb.ToString();
}这是我的方法,可能实际开发中LZ不需要写这么复杂,直接判断长度,如果长度超出截取就可以了<%#getSubString("这里是内容",40)%>
<div id="aa"><font color= "red "> 这里是内容 </font></div>document.getElementById("aa").innerText
<font color= "red "> 这里是内容 </font>开始截取的位置是第一次发现“>”,结束位置是第二次发现“<”
up
<script src="App_common/scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("font[color='red']").val();//输出结果:这里是内容
});
</script>
还有就是我想在后台截取
str = str.SubString(...);
var Varfont=$("font[color=red]").val();
$("#hidvar").val(Varfont);
后台:string ss=this.hidvar.value;
{
if (stringToSub == null || stringToSub.Length == 0 || length <= 0) return "";
Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
char[] stringChar = stringToSub.ToCharArray();
StringBuilder sb = new StringBuilder();
int nLength = 0;
bool isCut = false;
for (int i = 0; i < stringChar.Length; i++)
{
if (regex.IsMatch((stringChar[i]).ToString()))
{
sb.Append(stringChar[i]);
nLength += 2;
}
else
{
sb.Append(stringChar[i]);
nLength = nLength + 1;
}
{
string strOutput = Htmlstring; strOutput = Regex.Replace(strOutput, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
// strOutput = Regex.Replace(strOutput,@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>","",RegexOptions.IgnoreCase);//在个别情况下会引起IIS吃爆CPU的现象
strOutput = Regex.Replace(strOutput, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput, @"-->", "", RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput, @"<!--.*", "", RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput, @"&#(\d+);", "", RegexOptions.IgnoreCase);
strOutput.Replace("<", "");
strOutput.Replace(">", "");
strOutput.Replace("\r\n", "");
strOutput = HttpContext.Current.Server.HtmlEncode(strOutput).Trim();
strOutput=strOutput.Replace(" ", "");
return strOutput;
}