asp.net准确截取文本长度(取得标题的字符数)
来源:互联网 发布:易知投资 怎么样 编辑:程序博客网 时间:2024/05/17 21:42
asp.net中自带的CutString只能截取字符数量的长度,但中英文字符数有差异,一个中文字等同于二个英文字符的宽度,这样对截取后的效果不理想.使用以下的方法就能解决.
//调用方法
string title=BLL.CutStr.CutString("标题",10);
string title=BLL.CutStr.CutString("标题",10);
using System;
using System.Collections.Generic;
using System.Text;
namespace BLL
{
public class CutStr
{
public static string CutString(string str, int length)
{
if (str == "" )
{
str = "没有信息";
}
else
{
int i = 0, j = 0;
foreach (char chr in str)
{
if ((int)chr > 127)
{
i += 2;
}
else
{
i++;
}
if (i > length)
{
str = str.Substring(0, j) ;
//str = str.Substring(0, j) + "...";
break;
}
j++;
}
}
return str;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace BLL
{
public class CutStr
{
public static string CutString(string str, int length)
{
if (str == "" )
{
str = "没有信息";
}
else
{
int i = 0, j = 0;
foreach (char chr in str)
{
if ((int)chr > 127)
{
i += 2;
}
else
{
i++;
}
if (i > length)
{
str = str.Substring(0, j) ;
//str = str.Substring(0, j) + "...";
break;
}
j++;
}
}
return str;
}
}
}
- asp.net准确截取文本长度(取得标题的字符数)
- ASP按字符长度精准截取中英文标题
- Asp截取字符长度函数
- .net截取文本框字符数
- C#截取指定长度字符超出部分以"..."代替,用于处理过长的标题
- lua 含中文的字符串处理--分离字符、计算字符数、截取指定长度
- 截取一定长度的文章标题,超过指定长度,就加“...”,处理字符和汉字不等长问题
- asp.net中GridView绑定后的字符截取
- oracle取得数据的长度字节数
- ASP.NET控制输出内容的字符长度
- 截取字符串中英文字符的长度
- js截取固定长度的中英文字符
- 绑定数据截取固定的字符长度
- UNICODE 截取字符长度的方法
- 截取指定长度的字符内容
- 实时获取文本域中的长度,超出长度自动截取
- ASP截取字符 截取字符之间的字符
- 根据字节数获取字符长度 并截取
- InstallShield SQL 数据库安装
- AOI的基本原理
- 跟朋友之间AOI技术探讨---跟大家分享
- 离线式AOI的定位夹紧装置
- 线路板流程术语中英文对照
- asp.net准确截取文本长度(取得标题的字符数)
- 在大公司混,靠的不是智商而是情商
- joyfire linux 笔记
- Red Hat整合JBoss陆续加入图形化接口
- GNU通用公共许可证(GPL) 中文版
- 关于现在流行的测试工具 Reational Rose破解版安装方法(new)
- 简易论坛后台数据库设计及实现
- 伯乐很多,你是千里马吗?
- 软件测试自动化的一些具体做法