能实现
“现有新闻数据表news,其中有新闻标题title字段。
请问,我如何能将title字段中取出的数据,截取它的指定长度汉字,超出部分以"..."代替”
这也可以
“现有新闻数据表news,其中有新闻标题title字段。
请问,我如何能将title字段中取出的数据,截取它的指定长度汉字,超出部分以"..."代替”
这也可以
解决方案 »
- 在ISAPI_REWRITE中URL重定向怎么写?
- 二次开发一个latin1的MySQL要在里面加中文,但我插入的却是“?”号怎么办。。有C#代码
- 替代ViewState
- gridview中的数据导入Excel中,gridview是分页的
- self.location != top.location 是什么意思,有什么作用
- 使用Timer刷新label值的问题
- datagrid中字段的显示问题,急,在线等
- 郁闷啊~关于DropDownList的问题,我的程序没有错呀~~各位大侠救命,在线等~~这么晚了不知道还有人没有~~
- 如何用一个字符串数组存储多行文本框每行的数据???
- .NET能不能执行一段符合VB.NET的代码?
- ADO.Net 如何给数据表加排它锁?
- 问一个很菜鸟的问题。。AspNetPager分页控件怎么用?
/// <summary>
/// gridView字符过长截断显示
/// </summary>
/// <param name="GV">gridView</param>
/// <param name="num">字符显示长度</param>
public static void DataViewShow(GridView GV, int num)
{
for (int i = 0; i < GV.Rows.Count; i++)
{
for (int j = 0; j < GV.Columns.Count; j++)
{
GV.Rows[i].Cells[j].ToolTip = GV.Rows[i].Cells[j].Text;
if ((GV.Rows[i].Cells[j].Text).Length > num)
{
GV.Rows[i].Cells[j].Text = (GV.Rows[i].Cells[j].Text).Substring(0, num) + "...";
}
}
}
}
if(str.ToString().Length>8){
return str.ToString().Substring(0,8);
}
else return str.ToString()
}
string b;
if(a.length>10)
b=a.substring(0,10)+"...";
/// 将指定字符串按指定长度进行剪切,
/// </summary>
/// <param name="oldStr">需要截断的字符串</param>
/// <param name="maxLength">字符串的最大长度</param>
/// <param name="endWith">超过长度的后缀</param>
/// <returns>如果超过长度,返回截断后的新字符串加上后缀,否则,返回原字符串</returns>
public static string StringTruncat(string oldStr, int maxLength, string endWith)
{
if (string.IsNullOrEmpty(oldStr))
// throw new NullReferenceException("原字符串不能为空");
return oldStr + endWith;
if (maxLength < 1)
throw new Exception("返回的字符串长度必须大于[0]");
if (oldStr.Length > maxLength)
{
string strTmp = oldStr.Substring(0, maxLength);
if (string.IsNullOrEmpty(endWith))
return strTmp;
else
return strTmp + endWith;
}
return oldStr;
}调用:
<%# StringTruncat(Eval("Title").ToString(),20,"...")%>
这是什么意思?
这是什么意思?
=======================================================
明白了^-^
注意,是DataGrid,不是DataView
{
if(msg.length>7)
return msg.substring(0,7)+"...";
else
return msg+"...";
}
{display: block;width: 200px;overflow: hidden;white-space: nowrap;-o-text-overflow: ellipsis;text-overflow: ellipsis;}试着加一下这个CSS
我试了,不行呀
=================
有没有后台执行的,将截断的字符绑定到DataGrid中的?
.test1{display: block;width: 200px;overflow: hidden;white-space: nowrap;-o-text-overflow: ellipsis;text-overflow: ellipsis;}然后在你的代码里面给要绑定的地方指定class=test1width: 200px; 这个地方你要更改~ 改成你要的长度~ 否则当然没有效果了
</ul></body> </html>
/// 按字节数截取字符串,中文算两个字节,全角字符算两个字节,大写字母也算两个字节
/// </summary>
/// <param name="strSrc">要处理的字符串</param>
/// <param name="count">要保留的字节数</param>
/// <param name="withPoint">设置截取后加不加省略号('...') 默认的传true</param>
/// <returns></returns>
public static string GetStrByByteCount(string strSrc,int count,bool withPoint)
{
Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
char[] stringChar = strSrc.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())) || (stringChar[i] > 65280 && stringChar[i] < 65375) || (stringChar[i] > 64 && stringChar[i] < 91))
{
nLength += 2;
if (nLength > count)
{
isCut = true;
break;
}
sb.Append(stringChar[i]);
}
else
{
nLength = nLength + 1;
if (nLength > count)
{
isCut = true;
break;
}
sb.Append(stringChar[i]);
} }
if (isCut)
{
if (withPoint)
{
return sb.ToString() + "...";
}
else
{
return sb.ToString();
}
}
else
{
return sb.ToString();
}
}这个是我用的。中文和大写的算两个字节,还算准确。
前台绑定时加上这个方法就行了。
<ItemTemplate><div class="SubContent" style="width:50px"><asp:Label ID="labName" runat="server" /></div></ItemTemplate>
</asp:TemplateField>
.SubContent
{
text-overflow:ellipsis;
overflow:hidden;
white-space:nowrap;
word-break:keep-all;
}/*超出固定长度则多余部分会显示...*/
<ItemTemplatestyle="width:50px"><asp:Label ID="labName" runat="server" Text='<%#CTZ(Eval("绑定列名").ToString())%>' /></div></ItemTemplate>
</asp:TemplateField>后台
public string CTZ(string content)
{
if(content.Length>XX)//你所设定的字符长度
{
content.Substring(0,XX) ;
content+="....";
}
return content;
}
给分吧 LZ
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;/// <summary>
/// StringOperate 的摘要说明
/// </summary>
public class StringOperate
{
public StringOperate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void LeftSubstring(string str, int len)
{ }
public string CutString(string str, int len)
{
str = str.Trim();
byte[] myByte = System.Text.Encoding.Default.GetBytes(str);
if (myByte.Length > len)
{
string result = "";
for (int i = 0; i < str.Length; i++)
{
byte[] tempByte = System.Text.Encoding.Default.GetBytes(result);
if (tempByte.Length < len)
{
result += str.Substring(i, 1);
}
else
{
break;
}
}
return result + "...";
}
else
{
return str;
}
}
}
在需要控制字数的页面写一个方法:
//控制字数
public string ShowSubstring(string str, int number)
{
StringOperate newLiuYan = new StringOperate();
return newLiuYan.CutString(str, number); //number 为设定的显示字数
}在绑定时使用:
<%# ShowSubstring(DataBinder.Eval(Container.DataItem, "content").ToString(),18)%>
// 这里设为 18 个字
<asp:Label ID="lbl_Title" runat="server" Text='<%# (Eval("N_Title").ToString()).Length>21 ?((Eval("N_Title")).ToString()).Substring(0,21)+"...":Eval("N_Title") %>' ToolTip='<%# Eval("N_Title")%>'></asp:Label>大于21就那样了
新闻标题太长,截取字段,鼠标放上去显示全部
这样显示出来差别确实很大,这个怎样解决呢?
比如 ... 和 我我我这两个长度一样,显示效果可差远了,字符越多,差距越大啊.
/// <summary>
/// 按字符串实际长度截取定长字符窜
/// </summary>
/// <param name="str">原字符串</param>
/// <param name="length">要截取的长度</param>
/// <returns>string型字符串</returns>
public string CutString(string str, int length)
{
string delsqace = str.Trim();
int i = 0, j = 0;
foreach (char chr in delsqace)
{
if ((int)chr > 127)
{
i += 2;
}
else
{
i++;
}
if (i > length)
{
delsqace = delsqace.Substring(0, j);
break;
}
j++;
}
return delsqace; }
#endregion
这个就不会出现中文和英文差别较大的了
css这个值得学习
/// 字符串处理静态类
/// </summary>
public static class StringMangeCust
{
/// <summary>
/// 获取该字符串的字节数,按照英文一个字节,中文两个字节计算
/// </summary>
/// <param name="SourStr">需要计算的字符串</param>
/// <returns>字节数</returns>
public static int GetStringByteCount(string SourStr)
{
return System.Text.Encoding.Default.GetByteCount(SourStr);
}
/// <summary>
/// 返回一个字符串中的某个子字符串。
/// </summary>
/// <param name="SourStr">源字符串</param>
/// <param name="StrIndex">起始位置</param>
/// <param name="StrCount">子字符串的大小</param>
/// <returns></returns>
public static string SubString(string SourStr,int StrIndex, int StrCount)
{
byte[] TempBytes = System.Text.Encoding.Default.GetBytes(SourStr);
string Result = "";
int TempIndex = StrIndex;
int TempCount = StrCount;
int TempPlace=0;
if ((StrCount > 0) & (TempBytes.Length >= StrIndex))
{
while (TempPlace < TempBytes.Length)
{
if (TempBytes[TempPlace] > 127)
{
if (TempPlace == (TempIndex - 1))
{
TempIndex = TempIndex + 1;
TempPlace = TempPlace + 3;
}
else
{
TempPlace = TempPlace + 2;
}
if (TempPlace == (TempCount - 1))
{
TempPlace = TempPlace + 2;
TempCount = TempCount - 1;
}
}
else
{
TempPlace = TempPlace + 1;
}
}
if ((TempCount > 0) & (TempBytes.Length > StrIndex) & (TempBytes.Length>=(StrIndex+TempCount)))
{
Result = System.Text.Encoding.Default.GetString(TempBytes, StrIndex, StrCount);
}
}
return Result;
}
/// <summary>
/// 按照子字符串的大小获取一个子字符串。
/// </summary>
/// <param name="SourStr">源字符串</param>
/// <param name="StrCount">子字符串的大小</param>
/// <returns></returns>
public static string SubString(string SourStr, int StrCount)
{
return SubString(SourStr, 0, StrCount);
} /// <summary>
/// 获得16位MD5加密后的字符串。
/// </summary>
/// <param name="SourStr">需要加密的字符串</param>
/// <returns>加密后的md5字符串,用HEX格式显示</returns>
public static string getMd5Str(string SourStr)
{
MD5 m5Obj = MD5.Create();
string Result = "";
byte[] TempBytes = m5Obj.ComputeHash(Encoding.UTF8.GetBytes(SourStr));
foreach (Byte TempByte in TempBytes)
{
Result = Result + TempByte.ToString("X2");
}
return Result;
} /// <summary>
/// 获取一个层
/// </summary>
/// <param name="Left">层的left</param>
/// <param name="Top">曾的top</param>
/// <returns>返回层的html代码</returns>
public static string Ceate(string Left, string Top)
{
string Result = "";
Result="<div id=\"Layer1\" style=\"position:absolute; width:200px; height:115px; z-index:1\"></div>";
return Result;
}
/// <summary>
/// 判断一个字符串是否为实数,是实数返回true,否则返回false
/// </summary>
/// <param name="SourStr">字符串</param>
/// <returns>是实数返回true,否则返回false</returns>
public static bool CheckStrToNumberic(string SourStr)
{
if (SourStr.Trim() == "") return true;
try
{
Convert.ToDouble(SourStr);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 取字符串中第一个圆括号括起来的数字
/// </summary>
/// <param name="SourString">字符串</param>
/// <returns></returns>
public static string SubStringID(String SourString)
{
if (SourString.IndexOf(")") > 0)
{
return SourString.Substring(1, SourString.IndexOf(")") - 1);
}
else
{
return "-1";
}
}
}
select substring(title,1,20) from 表名
读出来后加...