ASP截取中英文字符串固定长度

时间:2009-08-19 17:12:00 

纯粹的截取字符串其实比较简单,用一个Left就搞定,但一个是全英文标题,一个是全中文标题,或中文混合排在一起,长短不一就很明显了,要考虑到中文跟英文的区别.

  本代码截取长度的计算方式:一个汉字长度计算为1个,一个英文字母或数字或符号的长度计算为0.5个.所以一个汉字占两个英文字母或数字或符号.如 "截取字符串"的长度="abcdefghij"的长度. 代码比较简单,呵呵....希望适合你用....

GetString.asp
<%
Function GetStringLength(txt,length)
dim i
i=1
y=0
txt=trim(txt)
for i=1 to len(txt)
j=mid(txt,i,1)
if asc(j)>=0 and asc(j)《=127 then '汉字外的其他符号,如:!@#,数字,大小写英文字母
y=y+0.5
else '汉字
y=y+1
end if
if -int(-y) >= length then '截取长度
txt = left(txt,i)
exit for
end if
next
response.write txt
End Function
%>

调用方法:
<%call GetStringLength(txt,length)%>
说明:txt为需截取的字符串,length为截取长度,一个汉字的长度按1个计算,一个英文或数字符号的长度按0.5个计算

把英文字母,数字,符号,汉字分开计算的长度截取:
GetString2.asp
<%
Function GetStringLength(txt,length)
dim i
i=1
y=0
txt=trim(txt)
for i=1 to len(txt)
j=mid(txt,i,1)
if asc(j)>=0 and asc(j)《=32 then '控制字符或通讯专用字符
y=y
elseif asc(j)>=33 and asc(j)<=47 then '标点符号 ! " # $ % & ' ( ) * + , - . /
y=y+0.5
elseif asc(j)>=48 and asc(j)<=57 then '数字 0123456789
y=y+0.5
num=num+1
elseif asc(j)>=58 and asc(j)<=64 then '标点符号 : ; 《 = > ? @
y=y+0.5
elseif asc(j)>=65 and asc(j)<=90 then '大写英文字母 ABCDEFGHIJKLMNOPQRSTUVWXYZ
y=y+0.5
beng=beng+1
elseif asc(j)>=91 and asc(j)<=96 then '标点符号 [ \ ] ^ _ `
y=y+0.5
elseif asc(j)>=97 and asc(j)<=122 then '小写英文字母 abcdefghijklmcopqrstuvwxyz
y=y+0.5
seng=seng+1
elseif asc(j)>=123 and asc(j)<=126 then '标点符号 { } ~
y=y+0.5
else
y=y+1
chn=chn+1
end if
if -int(-y) >= length then
txt = left(txt,i)
exit for
end if
next
End Function
%>

调用方法同上.

标签:asp,英文,字符串
0
投稿

猜你喜欢

  • 5个有趣的浏览器地址栏Javascript代码

    2008-07-21 13:04:00
  • 如何编写一个基于WEB的文件查询系统?

    2009-11-08 18:55:00
  • SqlServer中的日期与时间函数

    2011-11-03 17:12:34
  • asp如何在读取Excel文件时创建列表的下拉菜单?

    2010-06-18 19:59:00
  • wap开发 完整的WML文档结构详解

    2008-05-21 13:39:00
  • 用ASP对网页进行限制性的访问

    2008-07-03 13:02:00
  • 谈谈图片如何影响转换率

    2011-08-10 19:14:08
  • asp如何远程注册DLL

    2010-06-16 09:58:00
  • 提高MySQL查询效率的三个技巧

    2009-02-11 13:19:00
  • 王孟友教你如何设计标志(LOGO)

    2008-04-17 13:30:00
  • 树型结构在ASP中的简单解决

    2007-10-07 12:52:00
  • Flash对象在(x)HTML中的格式和参数及安全性

    2010-04-01 11:55:00
  • ASP编程中的常见问题

    2007-09-20 13:32:00
  • 向MySQL数据库的表中录入数据的实用方法

    2008-12-17 16:24:00
  • PHP结构型模式之代理模式

    2023-05-25 06:55:34
  • 用CSS3将你的设计带入下个高度[译]

    2009-06-22 13:03:00
  • SQL 注入式攻击的终极防范

    2011-04-03 11:21:00
  • ACCESS 2007出现“错误 '80040e14'“

    2008-06-19 13:21:00
  • sql server update 表的问题

    2009-10-04 20:32:00
  • 七十六个网站用户体验要点

    2010-08-11 14:52:00
  • asp之家 网络编程 m.aspxhome.com