.net如何截取字符串?跪地求解

kart15 2009-12-30 09:57:21
如题!
string str="<P>gdsfdsfdsfdsfds的飒飒的速度是的是电视上的</P>,<img src='images/1.gif'>审时度势的倒萨";

要求不截取html标记,

结果应该为"gdsfdsfdsfdsfds的飒飒的速度是的是电视上的,审时度势的倒萨"

请问大家这个应该怎么截取?
...全文
679 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
wh110 2009-12-31
  • 打赏
  • 举报
回复
路过顶一下,
有时候截取还是根据要求来写好点
wosizy 2009-12-31
  • 打赏
  • 举报
回复
我给你写了一个列子 我在自己电脑上跑了 没问题 结果就是你那效果
string ass = "<P>asdsfdsadsaasdfds的飒飒的速度是的是电视上的</P>";
string[] arr = ass.Split('<');
string tempa = "";
string temp = "";
string tem = "";
string te = "";
foreach (string ch in arr)
{
tempa += ch;
}
string[] brr = tempa.Split('P');
foreach(string bh in brr)
{
temp += bh;
}
string[] crr = temp.Split('>');
foreach (string ch in crr)
{
tem += ch;
}
string[] drr = tem.Split('/');
foreach(string dh in drr)
{
te += dh;
}
TextBox1.Text = te;
页面上TextBox1.Text的值是asdsfdsadsaasdfds的飒飒的速度是的是电视上的
虽然循环用的多 但是结果还是出来了
骑猪看海 2009-12-30
  • 打赏
  • 举报
回复
自己一封装的方法

/// <summary>
/// 清除HTML代码
/// </summary>
/// <param name="HTMLStr"></param>
/// <returns></returns>
public static string DelHtmlCode(string HTMLStr)
{
//方法一
return System.Text.RegularExpressions.Regex.Replace(HTMLStr, "<[^>]*>", "");
}

sugercgq 2009-12-30
  • 打赏
  • 举报
回复
strHtml = Regex.Replace(strHtml, @"<(.[^>]*)>", string.Empty, RegexOptions.IgnoreCase);
yan267 2009-12-30
  • 打赏
  • 举报
回复


public static string GetContentSummary(string content, int length, bool StripHTML)
{

string NowCheckLength = "";
if (length < content.Length)
{
NowCheckLength = "1";
}




if (string.IsNullOrEmpty(content) || length == 0)
return "";
if (StripHTML)
{
Regex re = new Regex("<[^>]*>");
content = re.Replace(content, "");
content = content.Replace(" ", "").Replace(" ", "");
if (content.Length <= length
)
return content;
else
CheckLeng = "1";
return content.Substring(0, length) + "……";
}
else
{


if (content.Length <= length)
return content;

int pos = 0, npos = 0, size = 0;
bool firststop = false, notr = false, noli = false;


StringBuilder sb = new StringBuilder();

while (true)
{
if (pos >= content.Length)
break;

string cur = content.Substring(pos, 1);

if (cur == "<")
{
string next = content.Substring(pos + 1, 3).ToLower();
if (next.IndexOf("p") == 0 && next.IndexOf("pre") != 0)
{
npos = content.IndexOf(">", pos) + 1;
}
else if (next.IndexOf("/p") == 0 && next.IndexOf("/pr") != 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
sb.Append("<br/>");
}
else if (next.IndexOf("br") == 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
sb.Append("<br/>");
}
else if (next.IndexOf("img") == 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
{
sb.Append(content.Substring(pos, npos - pos));
size += npos - pos + 1;
}
}
else if (next.IndexOf("li") == 0 || next.IndexOf("/li") == 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
{
sb.Append(content.Substring(pos, npos - pos));
}
else
{
if (!noli && next.IndexOf("/li") == 0)
{
sb.Append(content.Substring(pos, npos - pos));
noli = true;
}
}
}
else if (next.IndexOf("tr") == 0 || next.IndexOf("/tr") == 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
{
sb.Append(content.Substring(pos, npos - pos));
}
else
{
if (!notr && next.IndexOf("/tr") == 0)
{
sb.Append(content.Substring(pos, npos - pos));
notr = true;
}
}
}
else if (next.IndexOf("td") == 0 || next.IndexOf("/td") == 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
{
sb.Append(content.Substring(pos, npos - pos));
}
else
{
if (!notr)
{
sb.Append(content.Substring(pos, npos - pos));
}
}
}
else
{
npos = content.IndexOf(">", pos) + 1;
sb.Append(content.Substring(pos, npos - pos));
}
if (npos <= pos)
npos = pos + 1;
pos = npos;
}
else
{





if (size < length)
{
sb.Append(cur);
size++;

}
else
{
if (!firststop)
{
CheckLeng = "1";
//sb.Append("……");
firststop = true;
}
}
pos++;
}

}

string HTML = sb.ToString();
if (NoHTML(HTML).Length!=NoHTML(content).Length || CheckLeng == "1")
{
CheckLeng = "1";

HTML = HTML + "……";
}
return HTML;
}
}

public static string NoHTML(string Htmlstring)
{

//删除脚本

Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);

//删除HTML

Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);

Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);

Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);

Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);

Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);

Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);

Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);

Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);

Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);

Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);

Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);

Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);

Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);

Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);

Htmlstring.Replace("<", "");

Htmlstring.Replace(">", "");

Htmlstring.Replace("\r\n", "");

Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

return Htmlstring;

}





StripHTML 是否过滤html
这两个函数,可以保证截取的时候HTML是完整的,因为要保证一些颜色和样式的完整性搞出来的
tch198821 2009-12-30
  • 打赏
  • 举报
回复
同上
error_class 2009-12-30
  • 打赏
  • 举报
回复
稍复杂,用正则表达式 匹配吧。
随心录123 2009-12-30
  • 打赏
  • 举报
回复
我在仔细看了你的问题!你这个不是截取的问题!你这个是过滤里面标签的问题!
HttpUtility.HtmlEncode(FCKeditor1.Value.ToString());你后台存值的时候要用HttpUtility.HtmlEncode过滤标签!
前台取值的时候 再用HttpUtility.HtmlDecode("你的字段")

通过这样的方式 你存的是什么 你取出来的就是什么!你中间的图片(<img src='images/1.gif'>)也就显示了!
SamuDra 2009-12-30
  • 打赏
  • 举报
回复
学习学习。哈哈!
whb147 2009-12-30
  • 打赏
  • 举报
回复
看着跪的人,
就感觉很恶心
用得着吗?
飞驴 2009-12-30
  • 打赏
  • 举报
回复
substring(str1,1,1)
shuo20080722 2009-12-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yfqvip 的回复:]
C# codepublicstatic String delHtml(String inputString) {
String htmlStr= inputString;// 含html标签的字符串 String textStr="";
java.util.regex.Pattern p_script;
java.util.regex.?-
[/Quote]

顶,这个方法可以
newdigitime 2009-12-30
  • 打赏
  • 举报
回复
如果字符串格式是固定的.

直接
str=str.replace("<p>","");
str=str.replace("</p>","");
str=str.replace("<img src='images/1.gif'>","");
newdigitime 2009-12-30
  • 打赏
  • 举报
回复
这就需要用到正则表达式,将<>等替换成空,或者用正则,将标记之外的字符提取出来.
随心录123 2009-12-30
  • 打赏
  • 举报
回复
你怎么变你截取的都是最前面的几个啊!所以没有关系!
comzheng 2009-12-30
  • 打赏
  • 举报
回复
那就先去除html再截取了,如

public static string DropHTML(string strHtml)
{
string[] aryReg ={
@"<script[^>]*?>.*?</script>",

@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""''])(\\[""''tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
@"([\r\n])[\s]+",
@"&(quot|#34);",
@"&(amp|#38);",
@"&(lt|#60);",
@"&(gt|#62);",
@"&(nbsp|#160);",
@"&(iexcl|#161);",
@"&(cent|#162);",
@"&(pound|#163);",
@"&(copy|#169);",
@"&#(\d+);",
@"-->",
@"<!--.*\n"

};

string[] aryRep = {
"",
"",
"",
"\"",
"&",
"<",
">",
" ",
"\xa1",//chr(161),
"\xa2",//chr(162),
"\xa3",//chr(163),
"\xa9",//chr(169),
"",
"\r\n",
""
};

string newReg = aryReg[0];
string strOutput = strHtml;
for (int i = 0; i < aryReg.Length; i++)
{
Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
strOutput = regex.Replace(strOutput, aryRep[i]);
}

strOutput.Replace("<", "");
strOutput.Replace(">", "");
strOutput.Replace("\r\n", "");


return strOutput;
}





string NewString=DropHTML("需要截取的字符串").ToString().SubString(0,50);

随心录123 2009-12-30
  • 打赏
  • 举报
回复
<%#Eval("title").ToString().Length > 15 ? Eval("title").ToString().Substring(0, 15) + "....." : Eval("title")%>

title为你要截取的
gdsfdsfdsfdsfds的飒飒的速度是的是电视上的
截取的后
gdsfdsfdsfdsfds的飒飒......
vip__888 2009-12-30
  • 打赏
  • 举报
回复
用正则匹配
满衣兄 2009-12-30
  • 打赏
  • 举报
回复

public static String delHtml(String inputString) {
String htmlStr = inputString; // 含html标签的字符串
String textStr = "";
java.util.regex.Pattern p_script;
java.util.regex.Matcher m_script;
java.util.regex.Pattern p_html;
java.util.regex.Matcher m_html;

try {
String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式

String regEx_script = "<[\s]*?script[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?script[\s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[\s\S]*?<\/script>

p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 过滤script标签

p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 过滤html标签

textStr = htmlStr;

} catch (Exception e) {
System.err.println("Html2Text: " + e.getMessage());
}

return textStr;// 返回文本字符串
}
kart15 2009-12-30
  • 打赏
  • 举报
回复
str的长度随时会变的
加载更多回复(6)

62,265

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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