简单的问题

ajq1989 2010-10-20 02:39:54
在首页上显示新闻内容的时候。 超过了指定宽度如何用...表示或者不显示。

...全文
150 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hztltgg 2010-10-20
  • 打赏
  • 举报
回复

Imports System.Runtime.CompilerServices

Public Module StringExtensions

<Extension()> _
Public Function HalfSubstring(ByVal str As String, ByVal strLength As Integer) As String
strLength = strLength - 1
Dim newStr As String = String.Empty
Dim p As Integer

For i As Integer = 0 To str.Length - 1
Dim subStr As String = str.Substring(i, 1)
If (Regex.IsMatch(subStr, "[\u4e00-\u9fa5]")) Then
p += 2
newStr &= subStr
Else
p += 1
newStr &= subStr
End If
If p > strLength Then
If p > strLength + 1 And (Regex.IsMatch(subStr, "[\u4e00-\u9fa5]")) Then
newStr = newStr.Substring(0, newStr.Length - 1) & "."
Exit For
Else
If (Regex.IsMatch(newStr.Substring(newStr.Length - 1, 1), "[\u4e00-\u9fa5]")) Then
newStr = newStr.Substring(0, newStr.Length - 1) & ".."
Else
newStr = newStr.Substring(0, newStr.Length - 1) & "."
End If
Exit For
End If
End If
Next

Return newStr

End Function

End Module


hahanan1987 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ajq1989 的回复:]
除了以上写方法处理 。
不知道有么有一种方法是css的属性 超过就不显示的属性?
[/Quote]
css可以通过给定元素一个width,然后设置overflow:hidden来解决 不过这样要精确到像素控制
xia623330606 2010-10-20
  • 打赏
  • 举报
回复
在显示新闻字符串之前,用if判断字符串的Length属性是否大于X(你自己根据需要设置字符串的长度),如果大于在新闻字符串后+"..."就OK了
hongjiaoli 2010-10-20
  • 打赏
  • 举报
回复
调用:Truncation(stirngName,长度);
hongjiaoli 2010-10-20
  • 打赏
  • 举报
回复
public static string Truncation(string str, int length)
{
string retVal = str;
if (!string.IsNullOrEmpty(retVal))
{
if (str.Length <= length)
{
retVal = str;
}
else
{
retVal = str.Substring(0, length) + "...";
}
}
return retVal;
}

调用Truncation(stringName)
ajq1989 2010-10-20
  • 打赏
  • 举报
回复
除了以上写方法处理 。
不知道有么有一种方法是css的属性 超过就不显示的属性?
ZhuZhuYuXiang 2010-10-20
  • 打赏
  • 举报
回复
public static string Show(string str, int num)
{
try
{
if (str.Length > num)
{
return str.Substring(0, num) + "...";
}
else
{
return str;
}
}
catch (Exception ex)
{
System.Web.HttpContext.Current.Response.Write("<script>alert(\"" + ex.Message + "\")</script>");
return "";
}
}
hahanan1987 2010-10-20
  • 打赏
  • 举报
回复
写个函数处理下
//sourceStr字符串 //summaryCount保留的长度
public static string GetSummary(string sourceStr, int summaryCount)
{
if (!string.IsNullOrEmpty(sourceStr) && (sourceStr.Length > summaryCount))
{
return (sourceStr.Substring(0, summaryCount) + "...");
}
return sourceStr;
}
shaoliang520xi 2010-10-20
  • 打赏
  • 举报
回复
Eval("新闻").ToString().length>6?Eval("新闻").ToString().substring(0,6)+"...":Eval("新闻").ToString()
龍月 2010-10-20
  • 打赏
  • 举报
回复
查询的时候 数量限制

62,272

社区成员

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

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

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

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