当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 得到固定字符位置的函数

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 得到固定字符位置的函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-10   浏览: 486 ::
收藏到网摘: n/a

得到固定字符位置的函数

<Script language="Javascript" Runat="Server">
/*
=======================================================================================
 本程序段的功能:
 
 给定一个字符串,不管是英文还是中文,还是中英文混合的,只取前面的8个英文字母占位的宽度。
 关于该程序的说明:当最后一个字为中文,并且前面已经取得7位时,就不应该再取这个字了。再
 最后位置补空格; 否则的话,总共就会占9个英文字符的位置了。
 本程序由net_lover(孟子E章)编写,您可以任意使用本程序,但要保持本注释。
 有任何疑问请Email至: [email protected]
 欢迎访问:
 http://lucky.myrice.com
 http://colorweb.go.163.com
=======================================================================================
*/
function get8Length(str)

 var tmp = 0;
 var len = 0;
 var okLen = 0
 for(var i=0;i<8;i++)
 {
  if(str.charCodeAt(i)>255)
   tmp += 2 
  else
   len += 1
  okLen += 1
  if(tmp + len == 8)
  {
   return (str.substring(0,okLen));
   break;
  }
  if(tmp + len > 8)
  {
   return (str.substring(0,okLen - 1) + "&nbsp;"); 
   break;
  }
 }
}

</Script>
<%
Dim strTest
strTest = "我是孟子E章啊!!"
Response.write get8Length(strTest) & "……<br>"
strTest = "我是net_lover!!"
Response.write get8Length(strTest) & "……<br>"
strTest = "I我a是m孟子E章啊!!"
Response.write get8Length(strTest) & "……<br>"
%>